|
| 1 | +import { Server } from '@modelcontextprotocol/sdk/server'; |
| 2 | +import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; |
| 3 | +import { |
| 4 | + CallToolRequestSchema, |
| 5 | + ListToolsRequestSchema |
| 6 | +} from '@modelcontextprotocol/sdk/types.js'; |
| 7 | + |
| 8 | +import { forceRunAsync } from './run.js'; |
| 9 | + |
| 10 | +const GIT_NODE = new URL('../bin/git-node.js', import.meta.url).pathname; |
| 11 | + |
| 12 | +function capture(cmd, args) { |
| 13 | + return forceRunAsync(cmd, args, { |
| 14 | + captureStdout: true, |
| 15 | + captureStderr: true, |
| 16 | + ignoreFailure: false |
| 17 | + }); |
| 18 | +} |
| 19 | + |
| 20 | +const TOOLS = [ |
| 21 | + { |
| 22 | + name: 'git_node_metadata', |
| 23 | + description: 'Fetch metadata for a Node.js pull request ' + |
| 24 | + '(collaborators, CI status, reviews, etc.)', |
| 25 | + inputSchema: { |
| 26 | + type: 'object', |
| 27 | + properties: { |
| 28 | + pr: { |
| 29 | + type: 'string', |
| 30 | + description: 'Pull request URL or number, e.g. ' + |
| 31 | + 'https://github.com/nodejs/node/pull/12345 or 12345' |
| 32 | + } |
| 33 | + }, |
| 34 | + required: ['pr'] |
| 35 | + } |
| 36 | + }, |
| 37 | + { |
| 38 | + name: 'git_node_land', |
| 39 | + description: 'Land a Node.js pull request (interactive landing process)', |
| 40 | + inputSchema: { |
| 41 | + type: 'object', |
| 42 | + properties: { |
| 43 | + pr: { |
| 44 | + type: 'string', |
| 45 | + description: 'Pull request URL or number' |
| 46 | + }, |
| 47 | + yes: { |
| 48 | + type: 'boolean', |
| 49 | + description: 'Skip confirmation prompts (default: false)' |
| 50 | + } |
| 51 | + }, |
| 52 | + required: ['pr'] |
| 53 | + } |
| 54 | + }, |
| 55 | + { |
| 56 | + name: 'git_node_status', |
| 57 | + description: 'Show the status of a Node.js pull request landing', |
| 58 | + inputSchema: { |
| 59 | + type: 'object', |
| 60 | + properties: {}, |
| 61 | + required: [] |
| 62 | + } |
| 63 | + }, |
| 64 | + { |
| 65 | + name: 'ncu_ci', |
| 66 | + description: 'Check CI status for a Node.js pull request or commit', |
| 67 | + inputSchema: { |
| 68 | + type: 'object', |
| 69 | + properties: { |
| 70 | + pr: { |
| 71 | + type: 'string', |
| 72 | + description: 'Pull request URL or number' |
| 73 | + } |
| 74 | + }, |
| 75 | + required: ['pr'] |
| 76 | + } |
| 77 | + } |
| 78 | +]; |
| 79 | + |
| 80 | +export function createServer(captureImpl = capture) { |
| 81 | + const server = new Server( |
| 82 | + { name: 'ncu-mcp', version: '1.0.0' }, |
| 83 | + { capabilities: { tools: {} } } |
| 84 | + ); |
| 85 | + |
| 86 | + server.setRequestHandler(ListToolsRequestSchema, async() => ({ tools: TOOLS })); |
| 87 | + |
| 88 | + server.setRequestHandler(CallToolRequestSchema, async(request) => { |
| 89 | + const { name, arguments: args } = request.params; |
| 90 | + |
| 91 | + try { |
| 92 | + let output; |
| 93 | + |
| 94 | + switch (name) { |
| 95 | + case 'git_node_metadata': { |
| 96 | + output = await captureImpl('node', [GIT_NODE, 'metadata', String(args.pr)]); |
| 97 | + break; |
| 98 | + } |
| 99 | + case 'git_node_land': { |
| 100 | + const landArgs = ['land', String(args.pr)]; |
| 101 | + if (args.yes) landArgs.push('--yes'); |
| 102 | + output = await captureImpl('node', [GIT_NODE, ...landArgs]); |
| 103 | + break; |
| 104 | + } |
| 105 | + case 'git_node_status': { |
| 106 | + output = await captureImpl('node', [GIT_NODE, 'status']); |
| 107 | + break; |
| 108 | + } |
| 109 | + case 'ncu_ci': { |
| 110 | + output = await captureImpl('node', [GIT_NODE, 'metadata', '--ci', String(args.pr)]); |
| 111 | + break; |
| 112 | + } |
| 113 | + default: |
| 114 | + return { |
| 115 | + content: [{ type: 'text', text: `Unknown tool: ${name}` }], |
| 116 | + isError: true |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + return { |
| 121 | + content: [{ type: 'text', text: String(output) }] |
| 122 | + }; |
| 123 | + } catch (err) { |
| 124 | + return { |
| 125 | + content: [{ type: 'text', text: `Error: ${err.message}` }], |
| 126 | + isError: true |
| 127 | + }; |
| 128 | + } |
| 129 | + }); |
| 130 | + |
| 131 | + return server; |
| 132 | +} |
| 133 | + |
| 134 | +export async function run() { |
| 135 | + const server = createServer(); |
| 136 | + const transport = new StdioServerTransport(); |
| 137 | + await server.connect(transport); |
| 138 | +} |
0 commit comments