-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathserver.js
More file actions
129 lines (110 loc) · 3.72 KB
/
Copy pathserver.js
File metadata and controls
129 lines (110 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'use strict';
/**
* SigMap MCP server — zero npm dependencies.
*
* Wire protocol: JSON-RPC 2.0 over stdio.
* One JSON object per line on both stdin and stdout.
*
* Supported methods:
* initialize → serverInfo + capabilities
* tools/list → 3 tool definitions
* tools/call → dispatch to handler, return result
*/
const readline = require('readline');
const { TOOLS } = require('./tools');
const { readContext, searchSignatures, getMap, createCheckpoint, getRouting, explainFile, listModules, queryContext } = require('./handlers');
const SERVER_INFO = {
name: 'sigmap',
version: '2.3.0',
description: 'SigMap MCP server — code signatures on demand',
};
// ---------------------------------------------------------------------------
// JSON-RPC helpers
// ---------------------------------------------------------------------------
function respond(id, result) {
process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id, result }) + '\n');
}
function respondError(id, code, message) {
process.stdout.write(
JSON.stringify({ jsonrpc: '2.0', id, error: { code, message } }) + '\n'
);
}
// ---------------------------------------------------------------------------
// Method dispatcher
// ---------------------------------------------------------------------------
function dispatch(msg, cwd) {
const { method, id, params } = msg;
// Notifications (no id) need no response
if (method === 'notifications/initialized' || method === 'notifications/cancelled') {
return;
}
if (method === 'initialize') {
respond(id, {
protocolVersion: (params && params.protocolVersion) || '2024-11-05',
serverInfo: SERVER_INFO,
capabilities: { tools: {} },
});
return;
}
if (method === 'tools/list') {
respond(id, { tools: TOOLS });
return;
}
if (method === 'tools/call') {
const name = params && params.name;
const args = (params && params.arguments) || {};
let text;
try {
if (name === 'read_context') text = readContext(args, cwd);
else if (name === 'search_signatures') text = searchSignatures(args, cwd);
else if (name === 'get_map') text = getMap(args, cwd);
else if (name === 'create_checkpoint') text = createCheckpoint(args, cwd);
else if (name === 'get_routing') text = getRouting(args, cwd);
else if (name === 'explain_file') text = explainFile(args, cwd);
else if (name === 'list_modules') text = listModules(args, cwd);
else if (name === 'query_context') text = queryContext(args, cwd);
else {
respondError(id, -32601, `Unknown tool: ${name}`);
return;
}
} catch (err) {
respondError(id, -32603, `Tool error: ${err.message}`);
return;
}
respond(id, {
content: [{ type: 'text', text: String(text) }],
});
return;
}
// Unknown method
if (id !== undefined && id !== null) {
respondError(id, -32601, `Method not found: ${method}`);
}
}
// ---------------------------------------------------------------------------
// Server entry point
// ---------------------------------------------------------------------------
function start(cwd) {
const rl = readline.createInterface({ input: process.stdin, terminal: false });
rl.on('line', (line) => {
const trimmed = line.trim();
if (!trimmed) return;
let msg;
try {
msg = JSON.parse(trimmed);
} catch (_) {
// Cannot respond without a valid id — ignore malformed input
return;
}
try {
dispatch(msg, cwd);
} catch (err) {
const id = (msg && msg.id) != null ? msg.id : null;
respondError(id, -32603, `Internal error: ${err.message}`);
}
});
rl.on('close', () => {
process.exit(0);
});
}
module.exports = { start };