xmcp 0.6.13 → 0.7.1
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/dist/cli.js +8 -8
- package/dist/index.js +2 -2
- package/dist/runtime/adapter-express.js +2 -2
- package/dist/runtime/adapter-fastify.js +2 -2
- package/dist/runtime/adapter-nestjs.js +7 -7
- package/dist/runtime/adapter-nextjs.js +11 -11
- package/dist/runtime/http.js +3 -3
- package/dist/runtime/stdio.js +8 -8
- package/package.json +10 -11
- package/src/auth/api-key.ts +0 -120
- package/src/auth/jwt.ts +0 -52
- package/src/cli/commands/create/__tests__/create.test.ts +0 -99
- package/src/cli/commands/create/index.ts +0 -74
- package/src/cli/commands/create/templates/index.ts +0 -4
- package/src/cli/commands/create/templates/prompt.ts +0 -30
- package/src/cli/commands/create/templates/resource.ts +0 -20
- package/src/cli/commands/create/templates/tool.ts +0 -34
- package/src/cli/commands/create/templates/widget.ts +0 -33
- package/src/cli/utils/naming.ts +0 -39
- package/src/cli.ts +0 -125
- package/src/client/headers.ts +0 -61
- package/src/client/index.ts +0 -180
- package/src/client/types.ts +0 -122
- package/src/client/utils.ts +0 -133
- package/src/cloudflare.ts +0 -6
- package/src/compiler/build-telemetry.ts +0 -88
- package/src/compiler/client/component-compiler.ts +0 -250
- package/src/compiler/client/transpile.ts +0 -21
- package/src/compiler/compiler-context.ts +0 -61
- package/src/compiler/config/__tests__/config.test.ts +0 -459
- package/src/compiler/config/index.ts +0 -62
- package/src/compiler/config/injection.ts +0 -210
- package/src/compiler/config/schemas/bundler.ts +0 -12
- package/src/compiler/config/schemas/experimental/index.ts +0 -22
- package/src/compiler/config/schemas/index.ts +0 -20
- package/src/compiler/config/schemas/paths.ts +0 -35
- package/src/compiler/config/schemas/template.ts +0 -80
- package/src/compiler/config/schemas/transport/http.ts +0 -116
- package/src/compiler/config/schemas/transport/stdio.ts +0 -31
- package/src/compiler/config/schemas/typescript.ts +0 -26
- package/src/compiler/config/utils.ts +0 -168
- package/src/compiler/generate-env-code.ts +0 -5
- package/src/compiler/generate-import-code.ts +0 -187
- package/src/compiler/generate-tools-code.ts +0 -174
- package/src/compiler/get-bundler-config/get-entries.ts +0 -44
- package/src/compiler/get-bundler-config/get-externals.ts +0 -114
- package/src/compiler/get-bundler-config/get-injected-variables.ts +0 -46
- package/src/compiler/get-bundler-config/index.ts +0 -288
- package/src/compiler/get-bundler-config/plugins/index.ts +0 -140
- package/src/compiler/get-bundler-config/plugins/types/express.ts +0 -3
- package/src/compiler/get-bundler-config/plugins/types/fastify.ts +0 -4
- package/src/compiler/get-bundler-config/plugins/types/index.ts +0 -5
- package/src/compiler/get-bundler-config/plugins/types/nest.ts +0 -65
- package/src/compiler/get-bundler-config/plugins/types/next.ts +0 -52
- package/src/compiler/get-bundler-config/resolve-tsconfig-paths.ts +0 -51
- package/src/compiler/index.ts +0 -484
- package/src/compiler/on-first-build.ts +0 -54
- package/src/compiler/parse-xmcp-config.ts +0 -200
- package/src/compiler/start-http-server.ts +0 -42
- package/src/compiler/utils/__tests__/config-detection.test.ts +0 -307
- package/src/compiler/utils/__tests__/path-utils.test.ts +0 -137
- package/src/compiler/utils/config-detection.ts +0 -34
- package/src/compiler/utils/path-utils.ts +0 -22
- package/src/compiler/watcher-recovery.ts +0 -72
- package/src/host-bridge.ts +0 -36
- package/src/index.ts +0 -94
- package/src/platforms/build-cloudflare-output.ts +0 -116
- package/src/platforms/build-vercel-output.ts +0 -131
- package/src/plugins/index.ts +0 -1
- package/src/plugins/x402/index.ts +0 -24
- package/src/runtime/adapters/express/index.ts +0 -75
- package/src/runtime/adapters/fastify/__tests__/fastify.test.ts +0 -115
- package/src/runtime/adapters/fastify/index.ts +0 -78
- package/src/runtime/adapters/nestjs/index.ts +0 -16
- package/src/runtime/adapters/nestjs/xmcp.controller.ts +0 -34
- package/src/runtime/adapters/nestjs/xmcp.guard.ts +0 -101
- package/src/runtime/adapters/nestjs/xmcp.oauth.controller.ts +0 -36
- package/src/runtime/adapters/nestjs/xmcp.oauth.module.ts +0 -21
- package/src/runtime/adapters/nestjs/xmcp.oauth.service.ts +0 -36
- package/src/runtime/adapters/nestjs/xmcp.service.ts +0 -96
- package/src/runtime/adapters/nestjs/xmcp.utils.ts +0 -11
- package/src/runtime/adapters/nextjs/auth.ts +0 -265
- package/src/runtime/adapters/nextjs/handler/error-handler.ts +0 -67
- package/src/runtime/adapters/nextjs/handler/node-to-web-adapter.ts +0 -143
- package/src/runtime/adapters/nextjs/handler/request-converter.ts +0 -74
- package/src/runtime/adapters/nextjs/handler/server-lifecycle.ts +0 -69
- package/src/runtime/adapters/nextjs/index.ts +0 -82
- package/src/runtime/contexts/client-info-context.ts +0 -16
- package/src/runtime/contexts/http-request-context.ts +0 -21
- package/src/runtime/contexts/http-transport-context.ts +0 -18
- package/src/runtime/headers.ts +0 -42
- package/src/runtime/middlewares/utils.ts +0 -72
- package/src/runtime/platforms/cloudflare/cors.ts +0 -44
- package/src/runtime/platforms/cloudflare/middlewares/api-key.ts +0 -102
- package/src/runtime/platforms/cloudflare/middlewares/jwt.ts +0 -127
- package/src/runtime/platforms/cloudflare/types.ts +0 -23
- package/src/runtime/platforms/cloudflare/worker.ts +0 -270
- package/src/runtime/templates/home.ts +0 -746
- package/src/runtime/transports/http/base-streamable-http.ts +0 -46
- package/src/runtime/transports/http/cors/headers.ts +0 -58
- package/src/runtime/transports/http/cors/index.ts +0 -34
- package/src/runtime/transports/http/index.ts +0 -78
- package/src/runtime/transports/http/stateless-streamable-http.ts +0 -311
- package/src/runtime/transports/http/web-stateless-http.ts +0 -288
- package/src/runtime/transports/stdio/index.ts +0 -109
- package/src/runtime/utils/__tests__/client-info.test.ts +0 -146
- package/src/runtime/utils/__tests__/tool-extra.test.ts +0 -239
- package/src/runtime/utils/client-info.ts +0 -114
- package/src/runtime/utils/elicitation.ts +0 -396
- package/src/runtime/utils/ext-apps-registry.ts +0 -151
- package/src/runtime/utils/mcp-app-protocol.ts +0 -16
- package/src/runtime/utils/mcp-app-types.ts +0 -113
- package/src/runtime/utils/mcp-host-bridge.ts +0 -473
- package/src/runtime/utils/path-to-tool-name.ts +0 -69
- package/src/runtime/utils/prompt-loader.ts +0 -151
- package/src/runtime/utils/prompts.ts +0 -69
- package/src/runtime/utils/react/generate-html.ts +0 -69
- package/src/runtime/utils/react/index.ts +0 -6
- package/src/runtime/utils/request-tool-names.ts +0 -48
- package/src/runtime/utils/resource-loader.ts +0 -151
- package/src/runtime/utils/resources.ts +0 -331
- package/src/runtime/utils/server.ts +0 -107
- package/src/runtime/utils/tool-loader.ts +0 -149
- package/src/runtime/utils/tools.ts +0 -183
- package/src/runtime/utils/transformers/prompt.ts +0 -120
- package/src/runtime/utils/transformers/resource.ts +0 -196
- package/src/runtime/utils/transformers/tool.ts +0 -400
- package/src/runtime/utils/ui/__tests__/split-meta.test.ts +0 -803
- package/src/runtime/utils/ui/flatten-meta.ts +0 -44
- package/src/runtime/utils/ui/split-meta.ts +0 -78
- package/src/runtime/utils/utils/resource-uri-composer.ts +0 -204
- package/src/runtime/utils/validators/index.ts +0 -138
- package/src/telemetry/ci-info.ts +0 -6
- package/src/telemetry/debug.ts +0 -8
- package/src/telemetry/events/definitions.ts +0 -52
- package/src/telemetry/events/detached-flush.ts +0 -83
- package/src/telemetry/events/post-payload.ts +0 -141
- package/src/telemetry/events/tracker.ts +0 -281
- package/src/telemetry/index.ts +0 -7
- package/src/telemetry/metadata.ts +0 -49
- package/src/telemetry/project-id.ts +0 -35
- package/src/telemetry/storage.ts +0 -218
- package/src/types/adapter.d.ts +0 -5
- package/src/types/client-info.ts +0 -20
- package/src/types/declarations.ts +0 -37
- package/src/types/injected.d.ts +0 -62
- package/src/types/middleware.ts +0 -19
- package/src/types/prompt.ts +0 -6
- package/src/types/resource.ts +0 -15
- package/src/types/tool.ts +0 -192
- package/src/types/ui-meta.ts +0 -42
- package/src/utils/cli-icons.ts +0 -5
- package/src/utils/constants.ts +0 -31
- package/src/utils/context.ts +0 -105
- package/src/utils/detect-package-manager.ts +0 -46
- package/src/utils/file-watcher.ts +0 -66
- package/src/utils/fs-utils.ts +0 -16
- package/src/utils/path-validation.ts +0 -66
- package/src/utils/port-utils.ts +0 -43
- package/src/utils/spawn-process.ts +0 -49
- package/src/utils/subscribable.ts +0 -62
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { TemplateConfig } from "@/compiler/config/schemas";
|
|
2
|
-
import { AuthInfo } from "@modelcontextprotocol/sdk/server/auth/types";
|
|
3
|
-
import { Transport } from "@modelcontextprotocol/sdk/shared/transport";
|
|
4
|
-
import { MessageExtraInfo } from "@modelcontextprotocol/sdk/types";
|
|
5
|
-
import { IncomingMessage, ServerResponse } from "http";
|
|
6
|
-
|
|
7
|
-
export interface HttpTransportOptions {
|
|
8
|
-
port?: number;
|
|
9
|
-
host?: string;
|
|
10
|
-
endpoint?: string;
|
|
11
|
-
bodySizeLimit?: string;
|
|
12
|
-
debug?: boolean;
|
|
13
|
-
template?: TemplateConfig;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface JsonRpcMessage {
|
|
17
|
-
jsonrpc: string;
|
|
18
|
-
method?: string;
|
|
19
|
-
params?: any;
|
|
20
|
-
id?: string | number | null;
|
|
21
|
-
result?: any;
|
|
22
|
-
error?: any;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// shared between stateless and stateful transports
|
|
26
|
-
// just to paint the same interface
|
|
27
|
-
export abstract class BaseHttpServerTransport implements Transport {
|
|
28
|
-
// If we ever want to have more control over the transport, we can add onclose, onerror, onmessage here
|
|
29
|
-
// or interceptors for requests and responses
|
|
30
|
-
// e.g. to add a custom header to the response or a middleware layer!
|
|
31
|
-
|
|
32
|
-
// to do add the logging methods like log and error
|
|
33
|
-
// MCP SDK transport interface
|
|
34
|
-
onmessage?: (message: JsonRpcMessage, extra?: MessageExtraInfo) => void;
|
|
35
|
-
onerror?: (error: Error) => void;
|
|
36
|
-
onclose?: () => void;
|
|
37
|
-
|
|
38
|
-
abstract start(): Promise<void>;
|
|
39
|
-
abstract close(): Promise<void>;
|
|
40
|
-
abstract send(message: JsonRpcMessage): Promise<void>;
|
|
41
|
-
abstract handleRequest(
|
|
42
|
-
req: IncomingMessage & { auth?: AuthInfo },
|
|
43
|
-
res: ServerResponse,
|
|
44
|
-
parsedBody?: unknown
|
|
45
|
-
): Promise<void>;
|
|
46
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import type { CorsConfig } from "@/compiler/config";
|
|
2
|
-
|
|
3
|
-
export function buildCorsHeaders(
|
|
4
|
-
config: CorsConfig,
|
|
5
|
-
requestOrigin?: string | null
|
|
6
|
-
): Record<string, string> {
|
|
7
|
-
const headers: Record<string, string> = {};
|
|
8
|
-
|
|
9
|
-
if (config.origin !== undefined) {
|
|
10
|
-
if (config.origin === true) {
|
|
11
|
-
if (requestOrigin) {
|
|
12
|
-
headers["Access-Control-Allow-Origin"] = requestOrigin;
|
|
13
|
-
}
|
|
14
|
-
} else if (config.origin !== false) {
|
|
15
|
-
if (Array.isArray(config.origin)) {
|
|
16
|
-
const matchedOrigin =
|
|
17
|
-
requestOrigin && config.origin.includes(requestOrigin)
|
|
18
|
-
? requestOrigin
|
|
19
|
-
: config.origin[0] || "*";
|
|
20
|
-
headers["Access-Control-Allow-Origin"] = matchedOrigin;
|
|
21
|
-
} else {
|
|
22
|
-
headers["Access-Control-Allow-Origin"] = String(config.origin);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (config.methods !== undefined) {
|
|
28
|
-
headers["Access-Control-Allow-Methods"] = Array.isArray(config.methods)
|
|
29
|
-
? config.methods.join(",")
|
|
30
|
-
: String(config.methods);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (config.allowedHeaders !== undefined) {
|
|
34
|
-
headers["Access-Control-Allow-Headers"] = Array.isArray(
|
|
35
|
-
config.allowedHeaders
|
|
36
|
-
)
|
|
37
|
-
? config.allowedHeaders.join(",")
|
|
38
|
-
: String(config.allowedHeaders);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (config.exposedHeaders !== undefined) {
|
|
42
|
-
headers["Access-Control-Expose-Headers"] = Array.isArray(
|
|
43
|
-
config.exposedHeaders
|
|
44
|
-
)
|
|
45
|
-
? config.exposedHeaders.join(",")
|
|
46
|
-
: String(config.exposedHeaders);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (typeof config.credentials === "boolean") {
|
|
50
|
-
headers["Access-Control-Allow-Credentials"] = String(config.credentials);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (typeof config.maxAge === "number") {
|
|
54
|
-
headers["Access-Control-Max-Age"] = String(config.maxAge);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return headers;
|
|
58
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { CorsConfig } from "@/compiler/config";
|
|
2
|
-
import { ServerResponse } from "http";
|
|
3
|
-
import { Request, Response, NextFunction, RequestHandler } from "express";
|
|
4
|
-
import { buildCorsHeaders } from "./headers";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Sets CORS headers on the response based on the provided configuration.
|
|
8
|
-
* Accepts any Node ServerResponse (or Express Response, which extends it).
|
|
9
|
-
*/
|
|
10
|
-
export function setHeaders(
|
|
11
|
-
res: ServerResponse,
|
|
12
|
-
config: CorsConfig,
|
|
13
|
-
origin?: string
|
|
14
|
-
): void {
|
|
15
|
-
const headers = buildCorsHeaders(config, origin);
|
|
16
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
17
|
-
res.setHeader(key, value);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function cors(config: CorsConfig): RequestHandler {
|
|
22
|
-
return (req: Request, res: Response, next: NextFunction) => {
|
|
23
|
-
const requestOrigin = req.headers.origin;
|
|
24
|
-
|
|
25
|
-
setHeaders(res, config, requestOrigin);
|
|
26
|
-
|
|
27
|
-
if (req.method === "OPTIONS") {
|
|
28
|
-
res.status(204).end();
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
next();
|
|
33
|
-
};
|
|
34
|
-
}
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { createServer } from "../../utils/server";
|
|
2
|
-
import { StatelessStreamableHTTPTransport } from "./stateless-streamable-http";
|
|
3
|
-
import { Middleware } from "@/types/middleware";
|
|
4
|
-
import {
|
|
5
|
-
CorsConfig,
|
|
6
|
-
ResolvedHttpConfig,
|
|
7
|
-
TemplateConfig,
|
|
8
|
-
} from "@/compiler/config";
|
|
9
|
-
import { Provider, processProviders } from "@/runtime/middlewares/utils";
|
|
10
|
-
import { httpTransportContextProvider } from "@/runtime/contexts/http-transport-context";
|
|
11
|
-
import dotenv from "dotenv";
|
|
12
|
-
dotenv.config();
|
|
13
|
-
|
|
14
|
-
// by the time this is run, the config is already parsed and injected as object
|
|
15
|
-
// the injection handles the boolean case
|
|
16
|
-
|
|
17
|
-
const httpConfig = HTTP_CONFIG as ResolvedHttpConfig;
|
|
18
|
-
const corsConfig = HTTP_CORS_CONFIG as CorsConfig;
|
|
19
|
-
const templateConfig = TEMPLATE_CONFIG as TemplateConfig;
|
|
20
|
-
|
|
21
|
-
// middleware
|
|
22
|
-
const middleware = INJECTED_MIDDLEWARE as () =>
|
|
23
|
-
| Promise<{
|
|
24
|
-
default: Middleware | Middleware[];
|
|
25
|
-
}>
|
|
26
|
-
| undefined;
|
|
27
|
-
|
|
28
|
-
async function main() {
|
|
29
|
-
const options = {
|
|
30
|
-
port: httpConfig?.port,
|
|
31
|
-
host: httpConfig?.host,
|
|
32
|
-
debug: httpConfig?.debug,
|
|
33
|
-
bodySizeLimit: httpConfig?.bodySizeLimit?.toString(),
|
|
34
|
-
endpoint: httpConfig?.endpoint,
|
|
35
|
-
template: templateConfig,
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const corsOptions = {
|
|
39
|
-
origin: corsConfig.origin,
|
|
40
|
-
methods: corsConfig.methods,
|
|
41
|
-
allowedHeaders: corsConfig.allowedHeaders,
|
|
42
|
-
exposedHeaders: corsConfig.exposedHeaders,
|
|
43
|
-
credentials: corsConfig.credentials,
|
|
44
|
-
maxAge: corsConfig.maxAge,
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
let providers: Provider[] = [];
|
|
48
|
-
|
|
49
|
-
// process the middleware file content splitting into providers (middlewares and/or routers) preserving sequence
|
|
50
|
-
if (middleware) {
|
|
51
|
-
const middlewareModule = await middleware();
|
|
52
|
-
if (middlewareModule && middlewareModule.default) {
|
|
53
|
-
const defaultExport = middlewareModule.default;
|
|
54
|
-
|
|
55
|
-
providers = processProviders(defaultExport);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
httpTransportContextProvider(
|
|
60
|
-
{
|
|
61
|
-
config: {
|
|
62
|
-
http: httpConfig,
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
async () => {
|
|
66
|
-
const transport = new StatelessStreamableHTTPTransport(
|
|
67
|
-
createServer,
|
|
68
|
-
options,
|
|
69
|
-
corsOptions,
|
|
70
|
-
providers
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
await transport.start();
|
|
74
|
-
}
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
main();
|
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp";
|
|
2
|
-
import { StreamableHTTPServerTransport as SdkStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp";
|
|
3
|
-
import express, {
|
|
4
|
-
Express,
|
|
5
|
-
Request,
|
|
6
|
-
Response,
|
|
7
|
-
NextFunction,
|
|
8
|
-
RequestHandler,
|
|
9
|
-
} from "express";
|
|
10
|
-
import http, { IncomingMessage, ServerResponse } from "http";
|
|
11
|
-
import { randomUUID } from "node:crypto";
|
|
12
|
-
import type { TransportSendOptions } from "@modelcontextprotocol/sdk/shared/transport";
|
|
13
|
-
import {
|
|
14
|
-
BaseHttpServerTransport,
|
|
15
|
-
JsonRpcMessage,
|
|
16
|
-
HttpTransportOptions,
|
|
17
|
-
} from "./base-streamable-http";
|
|
18
|
-
import homeTemplate from "../../templates/home";
|
|
19
|
-
import { greenCheck } from "../../../utils/cli-icons";
|
|
20
|
-
import { findAvailablePort } from "../../../utils/port-utils";
|
|
21
|
-
import { cors } from "./cors";
|
|
22
|
-
import { Provider } from "@/runtime/middlewares/utils";
|
|
23
|
-
import {
|
|
24
|
-
httpRequestContextProvider,
|
|
25
|
-
setHttpRequestContext,
|
|
26
|
-
} from "@/runtime/contexts/http-request-context";
|
|
27
|
-
import {
|
|
28
|
-
extractToolNamesFromRequest,
|
|
29
|
-
storeToolNamesOnRequestHeaders,
|
|
30
|
-
} from "@/runtime/utils/request-tool-names";
|
|
31
|
-
import { CorsConfig, corsConfigSchema } from "@/compiler/config/schemas";
|
|
32
|
-
import { AuthInfo } from "@modelcontextprotocol/sdk/server/auth/types";
|
|
33
|
-
import { extractClientInfoFromMessages } from "@/runtime/utils/client-info";
|
|
34
|
-
|
|
35
|
-
// Global type declarations for tool name context
|
|
36
|
-
declare global {
|
|
37
|
-
var __XMCP_CURRENT_TOOL_NAME: string | string[] | undefined;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Stateless node transport that delegates protocol handling to the MCP SDK.
|
|
41
|
-
export class StatelessHttpServerTransport extends BaseHttpServerTransport {
|
|
42
|
-
private readonly debug: boolean;
|
|
43
|
-
private readonly transport: SdkStreamableHTTPServerTransport;
|
|
44
|
-
|
|
45
|
-
constructor(debug: boolean, _bodySizeLimit: string) {
|
|
46
|
-
super();
|
|
47
|
-
this.debug = debug;
|
|
48
|
-
this.transport = new SdkStreamableHTTPServerTransport({
|
|
49
|
-
sessionIdGenerator: undefined,
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
this.transport.onmessage = (message, extra) => {
|
|
53
|
-
this.onmessage?.(message as JsonRpcMessage, extra);
|
|
54
|
-
};
|
|
55
|
-
this.transport.onerror = (error) => {
|
|
56
|
-
this.onerror?.(error);
|
|
57
|
-
};
|
|
58
|
-
this.transport.onclose = () => {
|
|
59
|
-
this.onclose?.();
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
private log(message: string, ...args: unknown[]): void {
|
|
64
|
-
if (this.debug) {
|
|
65
|
-
console.log(`[StatelessHTTP] ${message}`, ...args);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async start(): Promise<void> {
|
|
70
|
-
await this.transport.start();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async close(): Promise<void> {
|
|
74
|
-
await this.transport.close();
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
async send(
|
|
78
|
-
message: JsonRpcMessage,
|
|
79
|
-
options?: TransportSendOptions
|
|
80
|
-
): Promise<void> {
|
|
81
|
-
await this.transport.send(message as any, options);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async handleRequest(
|
|
85
|
-
req: IncomingMessage & { auth?: AuthInfo },
|
|
86
|
-
res: ServerResponse,
|
|
87
|
-
parsedBody?: unknown
|
|
88
|
-
): Promise<void> {
|
|
89
|
-
this.log(`${req.method} ${req.url ?? req.headers.host ?? "/mcp"}`);
|
|
90
|
-
await this.transport.handleRequest(req, res, parsedBody);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// Stateless HTTP Transport wrapper
|
|
95
|
-
export class StatelessStreamableHTTPTransport {
|
|
96
|
-
private app: Express;
|
|
97
|
-
private server: http.Server;
|
|
98
|
-
private port: number;
|
|
99
|
-
private endpoint: string;
|
|
100
|
-
private debug: boolean;
|
|
101
|
-
private options: HttpTransportOptions;
|
|
102
|
-
private createServerFn: () => Promise<McpServer>;
|
|
103
|
-
private corsConfig: CorsConfig;
|
|
104
|
-
private providers: Provider[] | undefined;
|
|
105
|
-
|
|
106
|
-
constructor(
|
|
107
|
-
createServerFn: () => Promise<McpServer>,
|
|
108
|
-
options: HttpTransportOptions = {},
|
|
109
|
-
corsConfig: CorsConfig = corsConfigSchema.parse({}),
|
|
110
|
-
providers?: Provider[]
|
|
111
|
-
) {
|
|
112
|
-
this.options = {
|
|
113
|
-
...options,
|
|
114
|
-
};
|
|
115
|
-
this.app = express();
|
|
116
|
-
this.server = http.createServer(this.app);
|
|
117
|
-
this.port = options.port ?? parseInt(process.env.PORT || "3001", 10);
|
|
118
|
-
this.endpoint = options.endpoint ?? "/mcp";
|
|
119
|
-
this.debug = options.debug ?? false;
|
|
120
|
-
this.createServerFn = createServerFn;
|
|
121
|
-
this.corsConfig = corsConfig;
|
|
122
|
-
this.providers = providers;
|
|
123
|
-
|
|
124
|
-
// Setup JSON parsing middleware FIRST
|
|
125
|
-
this.app.use(express.json({ limit: this.options.bodySizeLimit || "10mb" }));
|
|
126
|
-
|
|
127
|
-
this.setupInitialRoutes();
|
|
128
|
-
this.setupInitialMiddleware();
|
|
129
|
-
|
|
130
|
-
this.setupProviders();
|
|
131
|
-
|
|
132
|
-
this.setupEndpointRoute();
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
private log(message: string, ...args: any[]): void {
|
|
136
|
-
if (this.debug) {
|
|
137
|
-
console.log(`[StatelessHTTP] ${message}`, ...args);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
private setupProviders(): void {
|
|
142
|
-
if (this.providers) {
|
|
143
|
-
for (const provider of this.providers) {
|
|
144
|
-
if (provider.router) {
|
|
145
|
-
this.app.use(provider.router);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (provider.middleware) {
|
|
149
|
-
this.app.use(provider.middleware);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
private setupInitialMiddleware(): void {
|
|
156
|
-
this.app.use(cors(this.corsConfig));
|
|
157
|
-
|
|
158
|
-
this.app.use((req: Request, _res: Response, next: NextFunction) => {
|
|
159
|
-
this.log(`${req.method} ${req.path}`);
|
|
160
|
-
next();
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
private setupInitialRoutes(): void {
|
|
165
|
-
this.app.get("/health", (_req: Request, res: Response) => {
|
|
166
|
-
res.status(200).json({
|
|
167
|
-
status: "ok",
|
|
168
|
-
transport: "streamable-http",
|
|
169
|
-
mode: "stateless",
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
this.app.get("/", (_req: Request, res: Response) => {
|
|
174
|
-
const customHomePage = this.options.template?.homePage;
|
|
175
|
-
|
|
176
|
-
if (customHomePage) {
|
|
177
|
-
res.send(customHomePage);
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
res.send(
|
|
182
|
-
homeTemplate(
|
|
183
|
-
this.endpoint,
|
|
184
|
-
this.options.template?.name,
|
|
185
|
-
this.options.template?.description
|
|
186
|
-
)
|
|
187
|
-
);
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
this.setupOpenAIAppsChallengeRoute();
|
|
191
|
-
|
|
192
|
-
// isolate requests context
|
|
193
|
-
this.app.use((req: Request, _res: Response, next: NextFunction) => {
|
|
194
|
-
const id = randomUUID();
|
|
195
|
-
httpRequestContextProvider({ id, headers: req.headers }, () => {
|
|
196
|
-
next();
|
|
197
|
-
});
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Exposes the OpenAI Apps verification challenge file when configured.
|
|
203
|
-
* The presence of OPENAI_APPS_VERIFICATION_TOKEN acts as the feature flag
|
|
204
|
-
*/
|
|
205
|
-
private setupOpenAIAppsChallengeRoute(): void {
|
|
206
|
-
const token = process.env.OPENAI_APPS_VERIFICATION_TOKEN;
|
|
207
|
-
|
|
208
|
-
if (!token) {
|
|
209
|
-
if (this.debug) {
|
|
210
|
-
this.log(
|
|
211
|
-
"OpenAI Apps verification token not configured; skipping challenge route"
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
return;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
this.app.get(
|
|
218
|
-
"/.well-known/openai-apps-challenge",
|
|
219
|
-
(_req: Request, res: Response) => {
|
|
220
|
-
res.status(200).set("Content-Type", "text/plain").send(token);
|
|
221
|
-
}
|
|
222
|
-
);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
private setupEndpointRoute(): void {
|
|
226
|
-
this.app.use(this.endpoint, async (req: Request, res: Response) => {
|
|
227
|
-
this.log(`${req.method} ${req.path}`);
|
|
228
|
-
|
|
229
|
-
this.extractAndStoreToolName(req);
|
|
230
|
-
|
|
231
|
-
await this.handleStatelessRequest(req, res);
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
private extractAndStoreToolName(req: Request): void {
|
|
236
|
-
try {
|
|
237
|
-
const toolNames = extractToolNamesFromRequest(req);
|
|
238
|
-
|
|
239
|
-
if (toolNames.length > 0) {
|
|
240
|
-
storeToolNamesOnRequestHeaders(req, toolNames);
|
|
241
|
-
global.__XMCP_CURRENT_TOOL_NAME = toolNames[0];
|
|
242
|
-
}
|
|
243
|
-
} catch (error) {
|
|
244
|
-
// no op
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
private async handleStatelessRequest(
|
|
249
|
-
req: Request,
|
|
250
|
-
res: Response
|
|
251
|
-
): Promise<void> {
|
|
252
|
-
try {
|
|
253
|
-
const requestClientInfo = extractClientInfoFromMessages(req.body);
|
|
254
|
-
const server = await this.createServerFn();
|
|
255
|
-
const transport = new StatelessHttpServerTransport(
|
|
256
|
-
this.debug,
|
|
257
|
-
this.options.bodySizeLimit || "10mb"
|
|
258
|
-
);
|
|
259
|
-
|
|
260
|
-
res.on("finish", () => {
|
|
261
|
-
global.__XMCP_CURRENT_TOOL_NAME = undefined;
|
|
262
|
-
});
|
|
263
|
-
res.on("close", () => {
|
|
264
|
-
void transport.close();
|
|
265
|
-
void server.close();
|
|
266
|
-
global.__XMCP_CURRENT_TOOL_NAME = undefined;
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
setHttpRequestContext({ clientInfo: requestClientInfo });
|
|
270
|
-
|
|
271
|
-
await server.connect(transport);
|
|
272
|
-
await transport.handleRequest(req, res, req.body);
|
|
273
|
-
} catch (error) {
|
|
274
|
-
console.error("[HTTP-server] Error handling MCP request:", error);
|
|
275
|
-
if (!res.headersSent) {
|
|
276
|
-
res.status(500).json({
|
|
277
|
-
jsonrpc: "2.0",
|
|
278
|
-
error: {
|
|
279
|
-
code: -32603,
|
|
280
|
-
message: "Internal server error",
|
|
281
|
-
},
|
|
282
|
-
id: null,
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
public async start(): Promise<void> {
|
|
289
|
-
const host = this.options.host || "127.0.0.1";
|
|
290
|
-
const port = await findAvailablePort(this.port, host);
|
|
291
|
-
|
|
292
|
-
this.server.listen(port, host, () => {
|
|
293
|
-
console.log(
|
|
294
|
-
`${greenCheck} MCP Server running on http://${host}:${port}${this.endpoint}`
|
|
295
|
-
);
|
|
296
|
-
|
|
297
|
-
this.setupShutdownHandlers();
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
private setupShutdownHandlers(): void {
|
|
302
|
-
process.on("SIGINT", this.shutdown.bind(this));
|
|
303
|
-
process.on("SIGTERM", this.shutdown.bind(this));
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
public shutdown(): void {
|
|
307
|
-
this.log("Shutting down server");
|
|
308
|
-
this.server.close();
|
|
309
|
-
process.exit(0);
|
|
310
|
-
}
|
|
311
|
-
}
|