-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmcp-stdio-server.ts
More file actions
166 lines (141 loc) · 3.87 KB
/
Copy pathmcp-stdio-server.ts
File metadata and controls
166 lines (141 loc) · 3.87 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import {
buildWorkbook,
createMcpWorkPaperToolServer,
type JsonRpcId,
type JsonRpcRequest,
type McpCapabilities,
type McpJsonRpcResponse,
} from './mcp-tool-server.ts'
interface InitializeResult {
protocolVersion: '2025-06-18'
capabilities: McpCapabilities
serverInfo: {
name: 'bilig-headless-workpaper-example'
version: '0.1.0'
}
}
interface JsonRpcErrorResponse {
jsonrpc: '2.0'
id: JsonRpcId
error: {
code: number
message: string
}
}
type InitializeResponse = {
jsonrpc: '2.0'
id: JsonRpcId | undefined
result: InitializeResult
}
type StdioJsonRpcResponse = JsonRpcErrorResponse | McpJsonRpcResponse | InitializeResponse
const server = createMcpWorkPaperToolServer(buildWorkbook())
let inputBuffer = ''
process.stdin.setEncoding('utf8')
process.stdin.on('data', (chunk: string) => {
inputBuffer += chunk
drainInputLines(false)
})
process.stdin.on('end', () => {
drainInputLines(true)
})
function drainInputLines(flush: boolean): void {
let newlineIndex = inputBuffer.indexOf('\n')
while (newlineIndex !== -1) {
const line = inputBuffer.slice(0, newlineIndex).trim()
inputBuffer = inputBuffer.slice(newlineIndex + 1)
if (line.length > 0) {
handleLine(line)
}
newlineIndex = inputBuffer.indexOf('\n')
}
const trailingLine = inputBuffer.trim()
if (flush && trailingLine.length > 0) {
inputBuffer = ''
handleLine(trailingLine)
}
}
function handleLine(line: string): void {
let request: JsonRpcRequest
try {
request = parseJsonRpcLine(line)
} catch (error) {
writeJsonRpcError(null, -32700, `Parse error: ${errorMessage(error)}`)
return
}
try {
const response = dispatchJsonRpc(request)
if (response !== undefined) {
writeJson(response)
}
} catch (error) {
writeJsonRpcError(request.id ?? null, -32603, errorMessage(error))
}
}
function parseJsonRpcLine(line: string): JsonRpcRequest {
const value: unknown = JSON.parse(line)
if (!isRecord(value)) {
throw new Error('Invalid JSON-RPC 2.0 request')
}
const candidate = value
if (candidate.jsonrpc !== '2.0' || typeof candidate.method !== 'string') {
throw new Error('Invalid JSON-RPC 2.0 request')
}
const id = candidate.id
if (id !== undefined && id !== null && typeof id !== 'string' && typeof id !== 'number') {
throw new Error(`Unsupported JSON-RPC id: ${JSON.stringify(id)}`)
}
return {
jsonrpc: '2.0',
id,
method: candidate.method,
params: parseOptionalRecord(candidate.params, 'params'),
}
}
function parseOptionalRecord(value: unknown, label: string): Record<string, unknown> | undefined {
if (value === undefined) {
return undefined
}
if (!isRecord(value)) {
throw new Error(`Expected ${label} to be an object`)
}
return value
}
function dispatchJsonRpc(request: JsonRpcRequest): InitializeResponse | McpJsonRpcResponse | undefined {
if (request.method === 'initialize') {
return {
jsonrpc: '2.0',
id: request.id,
result: {
protocolVersion: '2025-06-18',
capabilities: server.capabilities,
serverInfo: {
name: 'bilig-headless-workpaper-example',
version: '0.1.0',
},
},
}
}
if (request.method === 'notifications/initialized' || request.id === undefined) {
return undefined
}
return server.handleJsonRpc(request)
}
function writeJsonRpcError(id: JsonRpcId, code: number, message: string): void {
writeJson({
jsonrpc: '2.0',
id,
error: {
code,
message,
},
})
}
function writeJson(value: StdioJsonRpcResponse): void {
process.stdout.write(`${JSON.stringify(value)}\n`)
}
function errorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error)
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}