xmcp 0.6.6 → 0.6.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -0
- package/dist/cli.js +55 -36
- package/dist/compiler/config/index.d.ts +61 -54
- package/dist/compiler/config/schemas/template.d.ts +5 -0
- package/dist/compiler/config/schemas/transport/http.d.ts +82 -82
- package/dist/host-bridge.d.ts +4 -0
- package/dist/host-bridge.js +1 -0
- package/dist/index.d.ts +8 -2
- package/dist/index.js +5 -5
- package/dist/runtime/adapter-express.js +45 -26
- package/dist/runtime/adapter-nestjs.js +45 -26
- package/dist/runtime/adapter-nextjs.js +44 -25
- package/dist/runtime/contexts/client-info-context.d.ts +8 -0
- package/dist/runtime/contexts/http-request-context.d.ts +3 -1
- package/dist/runtime/headers.js +1 -1
- package/dist/runtime/http.js +53 -34
- package/dist/runtime/stdio.js +32 -24
- package/dist/runtime/utils/elicitation.d.ts +4 -0
- package/dist/runtime/utils/ext-apps-registry.d.ts +132 -0
- package/dist/runtime/utils/mcp-app-protocol.d.ts +14 -0
- package/dist/runtime/utils/mcp-app-types.d.ts +103 -0
- package/dist/runtime/utils/mcp-host-bridge.d.ts +43 -0
- package/dist/runtime/utils/path-to-tool-name.d.ts +20 -0
- package/dist/runtime/utils/prompt-loader.d.ts +11 -0
- package/dist/runtime/utils/prompts.d.ts +8 -0
- package/dist/runtime/utils/react/generate-html.d.ts +1 -0
- package/dist/runtime/utils/react/index.d.ts +4 -0
- package/dist/runtime/utils/resource-loader.d.ts +11 -0
- package/dist/runtime/utils/resources.d.ts +4 -0
- package/dist/runtime/utils/server.d.ts +37 -0
- package/dist/runtime/utils/tool-loader.d.ts +11 -0
- package/dist/runtime/utils/tools.d.ts +11 -0
- package/dist/runtime/utils/transformers/prompt.d.ts +32 -0
- package/dist/runtime/utils/transformers/resource.d.ts +26 -0
- package/dist/runtime/utils/transformers/tool.d.ts +30 -0
- package/dist/runtime/utils/ui/flatten-meta.d.ts +6 -0
- package/dist/runtime/utils/ui/split-meta.d.ts +17 -0
- package/dist/runtime/utils/utils/resource-uri-composer.d.ts +60 -0
- package/dist/runtime/utils/validators/index.d.ts +13 -0
- package/dist/types/client-info.d.ts +12 -0
- package/dist/types/tool.d.ts +69 -14
- package/dist/types/ui-meta.d.ts +15 -0
- package/dist/utils/context.d.ts +1 -1
- package/package.json +20 -1
- package/src/client/index.ts +4 -1
- package/src/compiler/client/component-compiler.ts +26 -5
- package/src/compiler/config/injection.ts +1 -0
- package/src/compiler/config/schemas/template.ts +13 -0
- package/src/compiler/generate-tools-code.ts +4 -6
- package/src/compiler/get-bundler-config/index.ts +1 -0
- package/src/compiler/get-bundler-config/plugins/index.ts +0 -1
- package/src/compiler/index.ts +24 -7
- package/src/host-bridge.ts +36 -0
- package/src/index.ts +44 -1
- package/src/runtime/adapters/express/index.ts +45 -47
- package/src/runtime/adapters/nestjs/xmcp.service.ts +4 -3
- package/src/runtime/adapters/nextjs/handler/node-to-web-adapter.ts +40 -6
- package/src/runtime/adapters/nextjs/handler/request-converter.ts +20 -0
- package/src/runtime/adapters/nextjs/handler/server-lifecycle.ts +10 -6
- package/src/runtime/adapters/nextjs/index.ts +41 -25
- package/src/runtime/contexts/client-info-context.ts +16 -0
- package/src/runtime/contexts/http-request-context.ts +2 -0
- package/src/runtime/headers.ts +37 -2
- package/src/runtime/platforms/cloudflare/cors.ts +0 -1
- package/src/runtime/platforms/cloudflare/worker.ts +46 -44
- package/src/runtime/transports/http/index.ts +0 -4
- package/src/runtime/transports/http/stateless-streamable-http.ts +131 -233
- package/src/runtime/transports/stdio/index.ts +40 -9
- package/src/runtime/utils/__tests__/client-info.test.ts +99 -0
- package/src/runtime/utils/__tests__/tool-extra.test.ts +149 -0
- package/src/runtime/utils/client-info.ts +78 -0
- package/src/runtime/utils/elicitation.ts +396 -0
- package/src/runtime/utils/mcp-app-protocol.ts +16 -0
- package/src/runtime/utils/mcp-app-types.ts +113 -0
- package/src/runtime/utils/mcp-host-bridge.ts +473 -0
- package/src/runtime/utils/prompt-loader.ts +151 -0
- package/src/runtime/utils/react/generate-html.ts +12 -4
- package/src/runtime/utils/resource-loader.ts +151 -0
- package/src/runtime/utils/resources.ts +0 -8
- package/src/runtime/utils/server.ts +31 -40
- package/src/runtime/utils/tool-loader.ts +149 -0
- package/src/runtime/utils/tools.ts +9 -6
- package/src/runtime/utils/transformers/tool.ts +35 -5
- package/src/runtime/utils/ui/__tests__/split-meta.test.ts +42 -0
- package/src/runtime/utils/ui/split-meta.ts +4 -1
- package/src/types/client-info.ts +12 -0
- package/src/types/injected.d.ts +62 -0
- package/src/types/tool.ts +99 -17
- package/src/types/ui-meta.ts +17 -1
- package/src/utils/context.ts +2 -2
|
@@ -18,9 +18,19 @@ export function nodeToWebAdapter(
|
|
|
18
18
|
const emitter = new EventEmitter();
|
|
19
19
|
let statusCode = 200;
|
|
20
20
|
let headers: OutgoingHttpHeaders | undefined;
|
|
21
|
-
|
|
21
|
+
const bodyChunks: Uint8Array[] = [];
|
|
22
22
|
let headersWritten = false;
|
|
23
23
|
let resolved = false;
|
|
24
|
+
let destroyed = false;
|
|
25
|
+
|
|
26
|
+
const appendBodyChunk = (chunk: Buffer | Uint8Array | string) => {
|
|
27
|
+
if (typeof chunk === "string") {
|
|
28
|
+
bodyChunks.push(Buffer.from(chunk));
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
bodyChunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
33
|
+
};
|
|
24
34
|
|
|
25
35
|
// Setup abort signal handling
|
|
26
36
|
signal.addEventListener("abort", () => {
|
|
@@ -44,20 +54,20 @@ export function nodeToWebAdapter(
|
|
|
44
54
|
return mockResponse;
|
|
45
55
|
},
|
|
46
56
|
|
|
47
|
-
write(chunk: Buffer | string): boolean {
|
|
57
|
+
write(chunk: Buffer | Uint8Array | string): boolean {
|
|
48
58
|
if (!resolved) {
|
|
49
|
-
|
|
59
|
+
appendBodyChunk(chunk);
|
|
50
60
|
}
|
|
51
61
|
return true;
|
|
52
62
|
},
|
|
53
63
|
|
|
54
|
-
end(data?: Buffer | string) {
|
|
64
|
+
end(data?: Buffer | Uint8Array | string) {
|
|
55
65
|
if (resolved) {
|
|
56
66
|
return mockResponse;
|
|
57
67
|
}
|
|
58
68
|
|
|
59
69
|
if (data) {
|
|
60
|
-
|
|
70
|
+
appendBodyChunk(data);
|
|
61
71
|
}
|
|
62
72
|
|
|
63
73
|
// Ensure headers were written (default to 200 if not)
|
|
@@ -70,7 +80,7 @@ export function nodeToWebAdapter(
|
|
|
70
80
|
|
|
71
81
|
// Create Web Response
|
|
72
82
|
resolve(
|
|
73
|
-
new Response(
|
|
83
|
+
new Response(Buffer.concat(bodyChunks), {
|
|
74
84
|
status: statusCode,
|
|
75
85
|
headers: headers as Record<string, string>,
|
|
76
86
|
})
|
|
@@ -85,6 +95,22 @@ export function nodeToWebAdapter(
|
|
|
85
95
|
return mockResponse;
|
|
86
96
|
},
|
|
87
97
|
|
|
98
|
+
off(event: string, listener: (...args: unknown[]) => void) {
|
|
99
|
+
emitter.off(event, listener);
|
|
100
|
+
return mockResponse;
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
removeListener(event: string, listener: (...args: unknown[]) => void) {
|
|
104
|
+
emitter.removeListener(event, listener);
|
|
105
|
+
return mockResponse;
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
destroy(_error?: Error) {
|
|
109
|
+
destroyed = true;
|
|
110
|
+
emitter.emit("close");
|
|
111
|
+
return mockResponse;
|
|
112
|
+
},
|
|
113
|
+
|
|
88
114
|
get statusCode(): number {
|
|
89
115
|
return statusCode;
|
|
90
116
|
},
|
|
@@ -96,6 +122,14 @@ export function nodeToWebAdapter(
|
|
|
96
122
|
get headersSent(): boolean {
|
|
97
123
|
return headersWritten;
|
|
98
124
|
},
|
|
125
|
+
|
|
126
|
+
get destroyed(): boolean {
|
|
127
|
+
return destroyed;
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
get writable(): boolean {
|
|
131
|
+
return !destroyed;
|
|
132
|
+
},
|
|
99
133
|
} as ServerResponse;
|
|
100
134
|
|
|
101
135
|
// Execute handler
|
|
@@ -10,6 +10,25 @@ export interface RequestConversionOptions {
|
|
|
10
10
|
auth?: AuthInfo;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
function toRawHeaders(headers: IncomingHttpHeaders): string[] {
|
|
14
|
+
const rawHeaders: string[] = [];
|
|
15
|
+
|
|
16
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
17
|
+
if (value === undefined) continue;
|
|
18
|
+
|
|
19
|
+
if (Array.isArray(value)) {
|
|
20
|
+
for (const item of value) {
|
|
21
|
+
rawHeaders.push(key, item);
|
|
22
|
+
}
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
rawHeaders.push(key, value);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return rawHeaders;
|
|
30
|
+
}
|
|
31
|
+
|
|
13
32
|
/**
|
|
14
33
|
* Creates a Node.js IncomingMessage from Web Request options.
|
|
15
34
|
* This adapter enables the MCP SDK to work with Web Request objects.
|
|
@@ -37,6 +56,7 @@ export function createIncomingMessage(
|
|
|
37
56
|
req.method = method;
|
|
38
57
|
req.url = url;
|
|
39
58
|
req.headers = headers;
|
|
59
|
+
req.rawHeaders = toRawHeaders(headers);
|
|
40
60
|
|
|
41
61
|
// Set auth if available (can be set by middleware)
|
|
42
62
|
if (auth) {
|
|
@@ -37,13 +37,17 @@ export function setupCleanupHandlers(
|
|
|
37
37
|
* Initializes and configures the MCP server with tools, prompts, and resources
|
|
38
38
|
*/
|
|
39
39
|
export async function initializeMcpServer(): Promise<McpServer> {
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
const
|
|
40
|
+
const toolModulesPromise = loadTools();
|
|
41
|
+
const promptModulesPromise = loadPrompts();
|
|
42
|
+
const resourceModulesPromise = loadResources();
|
|
43
|
+
const [toolModules, promptModules, resourceModules] = await Promise.all([
|
|
44
|
+
toolModulesPromise,
|
|
45
|
+
promptModulesPromise,
|
|
46
|
+
resourceModulesPromise,
|
|
47
|
+
]);
|
|
43
48
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const server = new McpServer(INJECTED_CONFIG);
|
|
49
|
+
const { instructions, ...serverInfo } = INJECTED_CONFIG;
|
|
50
|
+
const server = new McpServer(serverInfo, { instructions });
|
|
47
51
|
|
|
48
52
|
await configureServer(server, toolModules, promptModules, resourceModules);
|
|
49
53
|
|
|
@@ -10,6 +10,12 @@ import {
|
|
|
10
10
|
} from "./handler/server-lifecycle";
|
|
11
11
|
import { createIncomingMessage } from "./handler/request-converter";
|
|
12
12
|
import type { AuthInfo } from "@modelcontextprotocol/sdk/server/auth/types";
|
|
13
|
+
import { httpRequestContextProvider } from "@/runtime/contexts/http-request-context";
|
|
14
|
+
import { randomUUID } from "node:crypto";
|
|
15
|
+
import {
|
|
16
|
+
setHttpRequestContext,
|
|
17
|
+
} from "@/runtime/contexts/http-request-context";
|
|
18
|
+
import { extractClientInfoFromMessages } from "@/runtime/utils/client-info";
|
|
13
19
|
|
|
14
20
|
const BODY_SIZE_LIMIT = "10mb";
|
|
15
21
|
|
|
@@ -23,36 +29,46 @@ export async function xmcpHandler(request: Request): Promise<Response> {
|
|
|
23
29
|
return createMethodNotAllowedResponse();
|
|
24
30
|
}
|
|
25
31
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
32
|
+
const id = randomUUID();
|
|
33
|
+
const requestHeaders = Object.fromEntries(request.headers.entries());
|
|
34
|
+
|
|
35
|
+
return httpRequestContextProvider(
|
|
36
|
+
{ id, headers: requestHeaders, clientInfo: undefined },
|
|
37
|
+
() => {
|
|
38
|
+
return nodeToWebAdapter(request.signal, async (res: ServerResponse) => {
|
|
39
|
+
try {
|
|
40
|
+
// Initialize server and transport
|
|
41
|
+
const lifecycle = await createServerLifecycle(BODY_SIZE_LIMIT);
|
|
30
42
|
|
|
31
|
-
|
|
32
|
-
|
|
43
|
+
// Setup cleanup handlers
|
|
44
|
+
setupCleanupHandlers(res, lifecycle);
|
|
33
45
|
|
|
34
|
-
|
|
35
|
-
|
|
46
|
+
// Parse request body
|
|
47
|
+
const bodyContent = await request.json();
|
|
48
|
+
const clientInfo = extractClientInfoFromMessages(bodyContent);
|
|
49
|
+
setHttpRequestContext({ clientInfo });
|
|
36
50
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
51
|
+
// Convert Web Request to Node.js IncomingMessage
|
|
52
|
+
const incomingRequest = createIncomingMessage({
|
|
53
|
+
method: request.method,
|
|
54
|
+
url: request.url,
|
|
55
|
+
headers: requestHeaders,
|
|
56
|
+
auth: request.auth,
|
|
57
|
+
});
|
|
44
58
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
59
|
+
// Handle request through transport
|
|
60
|
+
await lifecycle.transport.handleRequest(
|
|
61
|
+
incomingRequest,
|
|
62
|
+
res,
|
|
63
|
+
bodyContent
|
|
64
|
+
);
|
|
65
|
+
} catch (error) {
|
|
66
|
+
console.error("[Next.js MCP] Error handling MCP request:", error);
|
|
67
|
+
sendInternalServerError(res);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
54
70
|
}
|
|
55
|
-
|
|
71
|
+
);
|
|
56
72
|
}
|
|
57
73
|
|
|
58
74
|
// Re-export auth types and handlers
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { McpClientInfo } from "@/types/client-info";
|
|
2
|
+
import { createContext } from "../../utils/context";
|
|
3
|
+
|
|
4
|
+
export interface ClientInfoContext {
|
|
5
|
+
clientInfo?: McpClientInfo;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const clientInfoContext = createContext<ClientInfoContext>({
|
|
9
|
+
name: "client-info-context",
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export const setClientInfoContext = clientInfoContext.setContext;
|
|
13
|
+
|
|
14
|
+
export const getClientInfoContext = clientInfoContext.getContext;
|
|
15
|
+
|
|
16
|
+
export const clientInfoContextProvider = clientInfoContext.provider;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createContext } from "../../utils/context";
|
|
2
|
+
import type { McpClientInfo } from "@/types/client-info";
|
|
2
3
|
|
|
3
4
|
// Headers type compatible with both Node.js IncomingHttpHeaders and Web API headers
|
|
4
5
|
export type HttpHeaders = Record<string, string | string[] | undefined>;
|
|
@@ -6,6 +7,7 @@ export type HttpHeaders = Record<string, string | string[] | undefined>;
|
|
|
6
7
|
export interface HttpRequestContext {
|
|
7
8
|
id: string;
|
|
8
9
|
headers: HttpHeaders;
|
|
10
|
+
clientInfo?: McpClientInfo;
|
|
9
11
|
}
|
|
10
12
|
|
|
11
13
|
export const httpRequestContext = createContext<HttpRequestContext>({
|
package/src/runtime/headers.ts
CHANGED
|
@@ -1,7 +1,42 @@
|
|
|
1
1
|
import { getHttpRequestContext } from "./contexts/http-request-context";
|
|
2
2
|
|
|
3
3
|
export const headers = () => {
|
|
4
|
-
const
|
|
4
|
+
const currentHeaders = getHttpRequestContext().headers;
|
|
5
|
+
const headerLookup = new Map<string, string>();
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
for (const key of Object.keys(currentHeaders)) {
|
|
8
|
+
const normalizedKey = key.toLowerCase();
|
|
9
|
+
|
|
10
|
+
if (!headerLookup.has(normalizedKey)) {
|
|
11
|
+
headerLookup.set(normalizedKey, key);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const resolveHeaderKey = (prop: string): string => {
|
|
16
|
+
return headerLookup.get(prop.toLowerCase()) ?? prop;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
return new Proxy(currentHeaders, {
|
|
20
|
+
get(target, prop, receiver) {
|
|
21
|
+
if (typeof prop !== "string") {
|
|
22
|
+
return Reflect.get(target, prop, receiver);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return Reflect.get(target, resolveHeaderKey(prop), receiver);
|
|
26
|
+
},
|
|
27
|
+
has(target, prop) {
|
|
28
|
+
if (typeof prop !== "string") {
|
|
29
|
+
return Reflect.has(target, prop);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return Reflect.has(target, resolveHeaderKey(prop));
|
|
33
|
+
},
|
|
34
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
35
|
+
if (typeof prop !== "string") {
|
|
36
|
+
return Reflect.getOwnPropertyDescriptor(target, prop);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return Reflect.getOwnPropertyDescriptor(target, resolveHeaderKey(prop));
|
|
40
|
+
},
|
|
41
|
+
});
|
|
7
42
|
};
|
|
@@ -6,7 +6,6 @@ import type { CorsConfig } from "@/compiler/config";
|
|
|
6
6
|
import { buildCorsHeaders } from "@/runtime/transports/http/cors/headers";
|
|
7
7
|
|
|
8
8
|
// CORS config - injected by compiler as combined object
|
|
9
|
-
// @ts-expect-error: injected by compiler
|
|
10
9
|
const corsConfig = HTTP_CORS_CONFIG as CorsConfig;
|
|
11
10
|
|
|
12
11
|
/**
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createServer } from "@/runtime/utils/server";
|
|
2
2
|
import { WebStatelessHttpTransport } from "@/runtime/transports/http/web-stateless-http";
|
|
3
3
|
import { httpRequestContextProvider } from "@/runtime/contexts/http-request-context";
|
|
4
|
+
import { extractClientInfoFromMessages } from "@/runtime/utils/client-info";
|
|
4
5
|
import homeTemplate from "../../templates/home";
|
|
5
6
|
|
|
6
7
|
import { addCorsHeaders, handleCorsPreflightRequest } from "./cors";
|
|
@@ -8,23 +9,20 @@ import type { Env, ExecutionContext } from "./types";
|
|
|
8
9
|
import type { WebMiddleware, WebMiddlewareContext } from "@/types/middleware";
|
|
9
10
|
import type { AuthInfo } from "@modelcontextprotocol/sdk/server/auth/types";
|
|
10
11
|
|
|
11
|
-
// @ts-expect-error: injected by compiler
|
|
12
12
|
const httpConfig = HTTP_CONFIG as {
|
|
13
13
|
port: number;
|
|
14
14
|
host: string;
|
|
15
|
-
bodySizeLimit:
|
|
15
|
+
bodySizeLimit: number;
|
|
16
16
|
endpoint: string;
|
|
17
17
|
debug: boolean;
|
|
18
18
|
};
|
|
19
19
|
|
|
20
|
-
// @ts-expect-error: injected by compiler
|
|
21
20
|
const templateConfig = TEMPLATE_CONFIG as {
|
|
22
21
|
name?: string;
|
|
23
22
|
description?: string;
|
|
24
23
|
homePage?: string;
|
|
25
24
|
};
|
|
26
25
|
|
|
27
|
-
// @ts-expect-error: injected by compiler
|
|
28
26
|
const middleware = INJECTED_MIDDLEWARE as
|
|
29
27
|
| (() => Promise<{ default?: WebMiddleware | WebMiddleware[] }>)
|
|
30
28
|
| undefined;
|
|
@@ -51,9 +49,7 @@ async function resolveWebMiddleware(): Promise<WebMiddleware[]> {
|
|
|
51
49
|
return resolvedMiddlewarePromise;
|
|
52
50
|
}
|
|
53
51
|
|
|
54
|
-
function normalizeWebMiddleware(
|
|
55
|
-
defaultExport: unknown
|
|
56
|
-
): WebMiddleware[] {
|
|
52
|
+
function normalizeWebMiddleware(defaultExport: unknown): WebMiddleware[] {
|
|
57
53
|
if (Array.isArray(defaultExport)) {
|
|
58
54
|
return defaultExport.filter(isWebMiddleware);
|
|
59
55
|
}
|
|
@@ -68,9 +64,7 @@ function normalizeWebMiddleware(
|
|
|
68
64
|
"middleware" in defaultExport &&
|
|
69
65
|
typeof (defaultExport as { middleware?: unknown }).middleware === "function"
|
|
70
66
|
) {
|
|
71
|
-
return [
|
|
72
|
-
(defaultExport as { middleware: WebMiddleware }).middleware,
|
|
73
|
-
];
|
|
67
|
+
return [(defaultExport as { middleware: WebMiddleware }).middleware];
|
|
74
68
|
}
|
|
75
69
|
|
|
76
70
|
return [];
|
|
@@ -125,6 +119,11 @@ async function handleMcpRequest(
|
|
|
125
119
|
authInfo?: AuthInfo
|
|
126
120
|
): Promise<Response> {
|
|
127
121
|
const requestId = crypto.randomUUID();
|
|
122
|
+
const requestPayload = await request
|
|
123
|
+
.clone()
|
|
124
|
+
.json()
|
|
125
|
+
.catch(() => undefined);
|
|
126
|
+
const clientInfo = extractClientInfoFromMessages(requestPayload);
|
|
128
127
|
|
|
129
128
|
// Use the http request context provider to maintain request isolation
|
|
130
129
|
return new Promise<Response>((resolve) => {
|
|
@@ -134,43 +133,46 @@ async function handleMcpRequest(
|
|
|
134
133
|
headers[key] = value;
|
|
135
134
|
});
|
|
136
135
|
|
|
137
|
-
httpRequestContextProvider(
|
|
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
|
-
} finally {
|
|
167
|
-
if (server && transport) {
|
|
168
|
-
ctx.waitUntil(
|
|
169
|
-
Promise.allSettled([transport.close(), server.close()])
|
|
136
|
+
httpRequestContextProvider(
|
|
137
|
+
{ id: requestId, headers, clientInfo },
|
|
138
|
+
async () => {
|
|
139
|
+
let server: Awaited<ReturnType<typeof createServer>> | null = null;
|
|
140
|
+
let transport: WebStatelessHttpTransport | null = null;
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
server = await createServer();
|
|
144
|
+
transport = new WebStatelessHttpTransport(httpConfig.debug);
|
|
145
|
+
|
|
146
|
+
await server.connect(transport);
|
|
147
|
+
const response = await transport.handleRequest(request, authInfo);
|
|
148
|
+
|
|
149
|
+
resolve(addCorsHeaders(response, requestOrigin));
|
|
150
|
+
} catch (error) {
|
|
151
|
+
console.error("[Cloudflare-MCP] Error handling MCP request:", error);
|
|
152
|
+
const errorResponse = new Response(
|
|
153
|
+
JSON.stringify({
|
|
154
|
+
jsonrpc: "2.0",
|
|
155
|
+
error: {
|
|
156
|
+
code: -32603,
|
|
157
|
+
message: "Internal server error",
|
|
158
|
+
},
|
|
159
|
+
id: null,
|
|
160
|
+
}),
|
|
161
|
+
{
|
|
162
|
+
status: 500,
|
|
163
|
+
headers: { "Content-Type": "application/json" },
|
|
164
|
+
}
|
|
170
165
|
);
|
|
166
|
+
resolve(addCorsHeaders(errorResponse, requestOrigin));
|
|
167
|
+
} finally {
|
|
168
|
+
if (server && transport) {
|
|
169
|
+
ctx.waitUntil(
|
|
170
|
+
Promise.allSettled([transport.close(), server.close()])
|
|
171
|
+
);
|
|
172
|
+
}
|
|
171
173
|
}
|
|
172
174
|
}
|
|
173
|
-
|
|
175
|
+
);
|
|
174
176
|
});
|
|
175
177
|
}
|
|
176
178
|
|
|
@@ -14,15 +14,11 @@ dotenv.config();
|
|
|
14
14
|
// by the time this is run, the config is already parsed and injected as object
|
|
15
15
|
// the injection handles the boolean case
|
|
16
16
|
|
|
17
|
-
// @ts-expect-error: injected by compiler
|
|
18
17
|
const httpConfig = HTTP_CONFIG as ResolvedHttpConfig;
|
|
19
|
-
// @ts-expect-error: injected by compiler
|
|
20
18
|
const corsConfig = HTTP_CORS_CONFIG as CorsConfig;
|
|
21
|
-
// @ts-expect-error: injected by compiler
|
|
22
19
|
const templateConfig = TEMPLATE_CONFIG as TemplateConfig;
|
|
23
20
|
|
|
24
21
|
// middleware
|
|
25
|
-
// @ts-expect-error: injected by compiler
|
|
26
22
|
const middleware = INJECTED_MIDDLEWARE as () =>
|
|
27
23
|
| Promise<{
|
|
28
24
|
default: Middleware | Middleware[];
|