-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcpProtocol.ts
More file actions
211 lines (188 loc) · 6.17 KB
/
Copy pathmcpProtocol.ts
File metadata and controls
211 lines (188 loc) · 6.17 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import type { McpToolDefinition } from './apiTools.js';
export const MODERN_PROTOCOL_VERSION = '2026-07-28';
export const LEGACY_PROTOCOL_VERSION = '2025-11-25';
const LEGACY_PROTOCOL_VERSIONS = new Set([
LEGACY_PROTOCOL_VERSION,
'2025-06-18',
'2025-03-26',
]);
const PROTOCOL_VERSION_META_KEY = 'io.modelcontextprotocol/protocolVersion';
const CLIENT_CAPABILITIES_META_KEY = 'io.modelcontextprotocol/clientCapabilities';
const SERVER_INFO_META_KEY = 'io.modelcontextprotocol/serverInfo';
type JsonRpcId = string | number;
export interface McpServerInfo {
name: string;
title: string;
version: string;
description: string;
websiteUrl?: string;
}
export interface McpServerPresentation {
serverInfo: McpServerInfo;
instructions: string;
}
export function createMcpServerPresentation(
brandName: string,
adminPanelOrigin?: string,
baseUrl = '',
): McpServerPresentation {
const adminPanelUrl = adminPanelOrigin
? new URL(baseUrl || '/', adminPanelOrigin).toString()
: undefined;
const adminPanel = `AdminForth admin panel for "${brandName}"${adminPanelUrl ? ` at ${adminPanelUrl}` : ''}`;
return {
serverInfo: {
name: 'adminforth-mcp',
title: `${brandName} Admin Panel`,
version: '1.0.0',
description: `${adminPanel}.`,
...(adminPanelUrl && { websiteUrl: adminPanelUrl }),
},
instructions: `This is the ${adminPanel}. Use its tools to read and update data allowed for the authenticated user.`,
};
}
export interface McpProtocolContext {
body: Record<string, any>;
headers: Record<string, any>;
serverInfo: McpServerInfo;
instructions: string;
listTools: () => McpToolDefinition[];
callTool: (
name: string,
arguments_: Record<string, unknown> | undefined,
) => Promise<{ output: unknown; isError: boolean }>;
}
export interface McpProtocolResponse {
status: number;
body?: Record<string, unknown>;
}
function errorResponse(
id: JsonRpcId | null,
code: number,
message: string,
status = 400,
): McpProtocolResponse {
return {
status,
body: { jsonrpc: '2.0', id, error: { code, message } },
};
}
function isModernRequest(body: Record<string, any>): boolean {
return body.params?._meta?.[PROTOCOL_VERSION_META_KEY] === MODERN_PROTOCOL_VERSION;
}
function completeResult(
result: Record<string, unknown>,
modern: boolean,
serverInfo: McpServerInfo,
) {
if (!modern) return result;
return {
resultType: 'complete',
...result,
_meta: {
...(result._meta as Record<string, unknown> | undefined),
[SERVER_INFO_META_KEY]: serverInfo,
},
};
}
function successResponse(
id: JsonRpcId,
result: Record<string, unknown>,
modern: boolean,
serverInfo: McpServerInfo,
): McpProtocolResponse {
return {
status: 200,
body: {
jsonrpc: '2.0',
id,
result: completeResult(result, modern, serverInfo),
},
};
}
function validateModernHeaders(
body: Record<string, any>,
headers: Record<string, any>,
): McpProtocolResponse | null {
const meta = body.params?._meta;
if (!meta || !(CLIENT_CAPABILITIES_META_KEY in meta)) {
return errorResponse(body.id ?? null, -32602, 'Missing required MCP request metadata.');
}
if (
headers['mcp-protocol-version'] !== MODERN_PROTOCOL_VERSION
|| headers['mcp-method'] !== body.method
|| (body.method === 'tools/call' && headers['mcp-name'] !== body.params?.name)
) {
return errorResponse(body.id ?? null, -32020, 'MCP request headers do not match the JSON-RPC body.');
}
return null;
}
function serializeToolOutput(output: unknown): string {
return typeof output === 'string' ? output : JSON.stringify(output, null, 2);
}
export async function handleMcpProtocol(
context: McpProtocolContext,
): Promise<McpProtocolResponse> {
const { body, headers, serverInfo, instructions } = context;
if (body.jsonrpc !== '2.0' || typeof body.method !== 'string') {
return errorResponse(body.id ?? null, -32600, 'Invalid JSON-RPC request.');
}
const modern = isModernRequest(body);
const requestedVersion = body.params?._meta?.[PROTOCOL_VERSION_META_KEY];
if (requestedVersion && requestedVersion !== MODERN_PROTOCOL_VERSION) {
return errorResponse(body.id ?? null, -32000, `Unsupported MCP protocol version: ${requestedVersion}`);
}
if (modern) {
const headerError = validateModernHeaders(body, headers);
if (headerError) return headerError;
}
if (body.id === undefined) {
return { status: 202 };
}
if (typeof body.id !== 'string' && typeof body.id !== 'number') {
return errorResponse(null, -32600, 'JSON-RPC id must be a string or number.');
}
switch (body.method) {
case 'server/discover':
if (!modern) {
return errorResponse(body.id, -32602, 'server/discover requires modern MCP request metadata.');
}
return successResponse(body.id, {
supportedVersions: [MODERN_PROTOCOL_VERSION],
capabilities: { tools: {} },
instructions,
ttlMs: 0,
cacheScope: 'private',
}, true, serverInfo);
case 'initialize': {
const requestedVersion = body.params?.protocolVersion;
return successResponse(body.id, {
protocolVersion: typeof requestedVersion === 'string' && LEGACY_PROTOCOL_VERSIONS.has(requestedVersion)
? requestedVersion
: LEGACY_PROTOCOL_VERSION,
capabilities: { tools: { listChanged: false } },
serverInfo,
instructions,
}, false, serverInfo);
}
case 'ping':
return successResponse(body.id, {}, modern, serverInfo);
case 'tools/list':
return successResponse(body.id, {
tools: context.listTools(),
...(modern && { ttlMs: 0, cacheScope: 'private' }),
}, modern, serverInfo);
case 'tools/call': {
if (typeof body.params?.name !== 'string') {
return errorResponse(body.id, -32602, 'Tool name is required.');
}
const result = await context.callTool(body.params.name, body.params.arguments);
return successResponse(body.id, {
content: [{ type: 'text', text: serializeToolOutput(result.output) }],
isError: result.isError,
}, modern, serverInfo);
}
default:
return errorResponse(body.id, -32601, `Method not found: ${body.method}`);
}
}