Files
orangecat/mcp-servers/git-server.js
CatoandClaude Opus 5.5 452d2b663c fix(mcp-servers): six of seven servers never started; convert them to ESM (#1491)
* fix(mcp-servers): six of seven servers never started; convert them to ESM

mcp-servers/package.json declares "type": "module", but fs, git, lint,
shell, supabase and vitest called require() and died on their first line
with "require is not defined in ES module scope". Only github-server.js,
which already used import, ran. They now use ESM imports, as
github-server.js does. The "plain CommonJS" lint waiver in two of them
was wrong and is removed, along with fs-server's unused fsSync import.

Once they started, every tool call that returned data failed too: the
results were { type: 'json', json: ... }, which is not an MCP content
type, so the SDK rejected them (-32602). They now return
{ type: 'text', text: JSON.stringify(..., null, 2) }, as github-server
does.

fs-server imports glob and supabase-server imports @supabase/supabase-js,
but neither was declared, so they resolved from the app's node_modules
only when it happened to be installed. Both are now declared. glob is
^14.0.3, because glob 11 carries GHSA-3g3m-2fjr-6pw4 and the minimatch
advisory; list_files works unchanged on 14.

New mcp-servers/smoke.mjs (npm run smoke): starts every *-server.js,
sends initialize, notifications/initialized and tools/list over stdio,
and calls one read-only tool on fs, git, lint and shell. Main: 1/7.
This branch: 7/7.

The rest of the diff in these files is eslint --fix and prettier, which
lint-staged applies to any staged file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9

* fix(mcp-servers): close stat-then-read race in fs-server (CodeQL)

read_file stat()ed the path, then readFile()d it again by name, so the
size check could apply to a different file than the one read (CodeQL
js/file-system-race, high). It now opens one handle and stats and reads
through it. delete_file had the same stat-then-unlink shape. unlink
never removes a directory, so it now calls unlink directly and maps
ENOENT to not_found and EISDIR/EPERM to the old refusal.

Checked over MCP: read ok, too-large refused, missing file errors,
delete ok then not_found, directory refused, path escape refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-31 05:32:48 +01:00

209 lines
6.6 KiB
JavaScript

#!/usr/bin/env node
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
import { spawn } from 'child_process';
import path from 'path';
function resolveCwd(cwd) {
const root = process.cwd();
const full = path.resolve(root, cwd || '.');
const rel = path.relative(root, full);
if (rel.startsWith('..') || path.isAbsolute(rel)) {
throw new Error('cwd escapes workspace');
}
return full;
}
function runGit(args, opt = {}) {
return new Promise(resolve => {
const child = spawn('git', args, { cwd: opt.cwd, env: process.env, shell: false });
let stdout = '';
let stderr = '';
child.stdout.on('data', d => {
stdout += d.toString();
});
child.stderr.on('data', d => {
stderr += d.toString();
});
child.on('close', code => resolve({ code, stdout, stderr }));
});
}
class GitServer {
constructor() {
this.server = new Server(
{ name: 'git-server', version: '1.0.0' },
{ capabilities: { tools: {} } }
);
this.setup();
}
setup() {
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: 'git_status',
description: 'Git status (porcelain)',
inputSchema: { type: 'object', properties: { cwd: { type: 'string' } } },
},
{
name: 'git_diff',
description: 'Git diff (optional path)',
inputSchema: {
type: 'object',
properties: {
cwd: { type: 'string' },
pathspec: { type: 'string' },
staged: { type: 'boolean', default: false },
},
},
},
{
name: 'git_branch_list',
description: 'List branches',
inputSchema: { type: 'object', properties: { cwd: { type: 'string' } } },
},
{
name: 'git_checkout',
description: 'Checkout a branch or ref',
inputSchema: {
type: 'object',
required: ['ref'],
properties: { cwd: { type: 'string' }, ref: { type: 'string' } },
},
},
{
name: 'git_create_branch',
description: 'Create and switch to a new branch',
inputSchema: {
type: 'object',
required: ['name'],
properties: {
cwd: { type: 'string' },
name: { type: 'string' },
startPoint: { type: 'string' },
},
},
},
{
name: 'git_add',
description: 'Stage files',
inputSchema: {
type: 'object',
required: ['paths'],
properties: {
cwd: { type: 'string' },
paths: { type: 'array', items: { type: 'string' } },
},
},
},
{
name: 'git_commit',
description: 'Commit staged changes',
inputSchema: {
type: 'object',
required: ['message'],
properties: { cwd: { type: 'string' }, message: { type: 'string' } },
},
},
{
name: 'git_push',
description: 'Push to remote',
inputSchema: {
type: 'object',
properties: {
cwd: { type: 'string' },
remote: { type: 'string', default: 'origin' },
branch: { type: 'string' },
setUpstream: { type: 'boolean', default: false },
},
},
},
],
}));
this.server.setRequestHandler(CallToolRequestSchema, async req => {
const name = req.params.name;
const a = req.params.arguments || {};
try {
const cwd = resolveCwd(a.cwd || '.');
switch (name) {
case 'git_status': {
const r = await runGit(['status', '--porcelain', '-b'], { cwd });
return {
content: [
{
type: 'text',
text: JSON.stringify(
{ code: r.code, stdout: r.stdout, stderr: r.stderr },
null,
2
),
},
],
};
}
case 'git_diff': {
const args = a.staged ? ['diff', '--staged'] : ['diff'];
if (a.pathspec) {
args.push(a.pathspec);
}
const r = await runGit(args, { cwd });
return { content: [{ type: 'text', text: r.stdout || r.stderr }] };
}
case 'git_branch_list': {
const r = await runGit(['branch', '--all', '--verbose', '--no-abbrev'], { cwd });
return { content: [{ type: 'text', text: r.stdout || r.stderr }] };
}
case 'git_checkout': {
const r = await runGit(['checkout', a.ref], { cwd });
return { content: [{ type: 'text', text: r.stdout || r.stderr }] };
}
case 'git_create_branch': {
const args = ['checkout', '-b', a.name];
if (a.startPoint) {
args.push(a.startPoint);
}
const r = await runGit(args, { cwd });
return { content: [{ type: 'text', text: r.stdout || r.stderr }] };
}
case 'git_add': {
const paths = Array.isArray(a.paths) && a.paths.length ? a.paths : ['.'];
const r = await runGit(['add', ...paths], { cwd });
return { content: [{ type: 'text', text: r.stdout || r.stderr || 'ok' }] };
}
case 'git_commit': {
const r = await runGit(['commit', '-m', a.message], { cwd });
return { content: [{ type: 'text', text: r.stdout || r.stderr }] };
}
case 'git_push': {
const args = ['push'];
if (a.setUpstream && a.branch) {
args.push('-u', a.remote || 'origin', a.branch);
}
const r = await runGit(args, { cwd });
return { content: [{ type: 'text', text: r.stdout || r.stderr }] };
}
default:
return { content: [{ type: 'text', text: `Unknown tool: ${name}` }], isError: true };
}
} catch (err) {
return {
content: [{ type: 'text', text: `❌ ${name} error: ${err?.message || String(err)}` }],
isError: true,
};
}
});
}
async run() {
const transport = new StdioServerTransport();
await this.server.connect(transport);
console.error('Git MCP Server running on stdio');
}
}
new GitServer().run();