diff --git a/src/mcp/github-file-ops-server.ts b/src/mcp/github-file-ops-server.ts index 8bc1bfb..80f579b 100644 --- a/src/mcp/github-file-ops-server.ts +++ b/src/mcp/github-file-ops-server.ts @@ -4,6 +4,7 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; import { readFile } from "fs/promises"; +import { join } from "path"; import fetch from "node-fetch"; import { GITHUB_API_URL } from "../github/api/config"; @@ -36,6 +37,7 @@ type GitHubNewCommit = { const REPO_OWNER = process.env.REPO_OWNER; const REPO_NAME = process.env.REPO_NAME; const BRANCH_NAME = process.env.BRANCH_NAME; +const REPO_DIR = process.env.REPO_DIR || process.cwd(); if (!REPO_OWNER || !REPO_NAME || !BRANCH_NAME) { console.error( @@ -71,18 +73,11 @@ server.tool( throw new Error("GITHUB_TOKEN environment variable is required"); } - // Convert absolute paths to relative if they match CWD - const cwd = process.cwd(); + // Process file paths - keep them as-is for now const processedFiles = files.map((filePath) => { + // Remove leading slash if present to ensure relative paths if (filePath.startsWith("/")) { - if (filePath.startsWith(cwd)) { - // Strip CWD from absolute path - return filePath.slice(cwd.length + 1); - } else { - throw new Error( - `Path '${filePath}' must be relative to repository root or within current working directory`, - ); - } + return filePath.slice(1); } return filePath; }); @@ -126,7 +121,12 @@ server.tool( // 3. Create tree entries for all files const treeEntries = await Promise.all( processedFiles.map(async (filePath) => { - const content = await readFile(filePath, "utf-8"); + // Construct the full path using REPO_DIR + const fullPath = filePath.startsWith('/') + ? filePath + : join(REPO_DIR, filePath); + + const content = await readFile(fullPath, "utf-8"); return { path: filePath, mode: "100644", @@ -232,13 +232,15 @@ server.tool( ], }; } catch (error) { + const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: "text", - text: `Error: ${error instanceof Error ? error.message : String(error)}`, + text: `Error: ${errorMessage}`, }, ], + error: errorMessage, isError: true, }; } @@ -423,13 +425,15 @@ server.tool( ], }; } catch (error) { + const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: "text", - text: `Error: ${error instanceof Error ? error.message : String(error)}`, + text: `Error: ${errorMessage}`, }, ], + error: errorMessage, isError: true, }; } diff --git a/src/mcp/install-mcp-server.ts b/src/mcp/install-mcp-server.ts index 7c271cf..dacae25 100644 --- a/src/mcp/install-mcp-server.ts +++ b/src/mcp/install-mcp-server.ts @@ -34,6 +34,7 @@ export async function prepareMcpConfig( REPO_OWNER: owner, REPO_NAME: repo, BRANCH_NAME: branch, + REPO_DIR: process.env.GITHUB_WORKSPACE || process.cwd(), // Use GitHub workspace directory }, }, },