@diffra-ai/mcp 0.0.0-stage → 0.1.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/CHANGELOG.md +9 -0
- package/LICENSE +22 -0
- package/README.md +190 -2
- package/THIRD_PARTY_NOTICES/modelcontextprotocol-typescript-sdk.txt +21 -0
- package/dist/auth-provider.d.ts +19 -0
- package/dist/auth-provider.d.ts.map +1 -0
- package/dist/auth-provider.js +2 -0
- package/dist/auth-provider.js.map +1 -0
- package/dist/client.d.ts +89 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +461 -0
- package/dist/client.js.map +1 -0
- package/dist/config.d.ts +117 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +153 -0
- package/dist/config.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/input-schema.d.ts +7 -0
- package/dist/input-schema.d.ts.map +1 -0
- package/dist/input-schema.js +13 -0
- package/dist/input-schema.js.map +1 -0
- package/dist/model-content.d.ts +19 -0
- package/dist/model-content.d.ts.map +1 -0
- package/dist/model-content.js +67 -0
- package/dist/model-content.js.map +1 -0
- package/dist/naming.d.ts +23 -0
- package/dist/naming.d.ts.map +1 -0
- package/dist/naming.js +37 -0
- package/dist/naming.js.map +1 -0
- package/dist/oauth/auth-provider.d.ts +52 -0
- package/dist/oauth/auth-provider.d.ts.map +1 -0
- package/dist/oauth/auth-provider.js +97 -0
- package/dist/oauth/auth-provider.js.map +1 -0
- package/dist/oauth/callback.d.ts +48 -0
- package/dist/oauth/callback.d.ts.map +1 -0
- package/dist/oauth/callback.js +119 -0
- package/dist/oauth/callback.js.map +1 -0
- package/dist/oauth/client-settings.d.ts +107 -0
- package/dist/oauth/client-settings.d.ts.map +1 -0
- package/dist/oauth/client-settings.js +114 -0
- package/dist/oauth/client-settings.js.map +1 -0
- package/dist/oauth/discovery.d.ts +30 -0
- package/dist/oauth/discovery.d.ts.map +1 -0
- package/dist/oauth/discovery.js +156 -0
- package/dist/oauth/discovery.js.map +1 -0
- package/dist/oauth/errors.d.ts +32 -0
- package/dist/oauth/errors.d.ts.map +1 -0
- package/dist/oauth/errors.js +58 -0
- package/dist/oauth/errors.js.map +1 -0
- package/dist/oauth/flow.d.ts +112 -0
- package/dist/oauth/flow.d.ts.map +1 -0
- package/dist/oauth/flow.js +360 -0
- package/dist/oauth/flow.js.map +1 -0
- package/dist/oauth/index.d.ts +11 -0
- package/dist/oauth/index.d.ts.map +1 -0
- package/dist/oauth/index.js +9 -0
- package/dist/oauth/index.js.map +1 -0
- package/dist/oauth/loopback.d.ts +51 -0
- package/dist/oauth/loopback.d.ts.map +1 -0
- package/dist/oauth/loopback.js +98 -0
- package/dist/oauth/loopback.js.map +1 -0
- package/dist/oauth/provider.d.ts +62 -0
- package/dist/oauth/provider.d.ts.map +1 -0
- package/dist/oauth/provider.js +118 -0
- package/dist/oauth/provider.js.map +1 -0
- package/dist/oauth/sign-in.d.ts +73 -0
- package/dist/oauth/sign-in.d.ts.map +1 -0
- package/dist/oauth/sign-in.js +182 -0
- package/dist/oauth/sign-in.js.map +1 -0
- package/dist/oauth/types.d.ts +83 -0
- package/dist/oauth/types.d.ts.map +1 -0
- package/dist/oauth/types.js +113 -0
- package/dist/oauth/types.js.map +1 -0
- package/dist/output-limit.d.ts +24 -0
- package/dist/output-limit.d.ts.map +1 -0
- package/dist/output-limit.js +58 -0
- package/dist/output-limit.js.map +1 -0
- package/dist/protocol/content.d.ts +83 -0
- package/dist/protocol/content.d.ts.map +1 -0
- package/dist/protocol/content.js +41 -0
- package/dist/protocol/content.js.map +1 -0
- package/dist/protocol/jsonrpc.d.ts +59 -0
- package/dist/protocol/jsonrpc.d.ts.map +1 -0
- package/dist/protocol/jsonrpc.js +67 -0
- package/dist/protocol/jsonrpc.js.map +1 -0
- package/dist/protocol/types.d.ts +123 -0
- package/dist/protocol/types.d.ts.map +1 -0
- package/dist/protocol/types.js +7 -0
- package/dist/protocol/types.js.map +1 -0
- package/dist/result-schema.d.ts +10 -0
- package/dist/result-schema.d.ts.map +1 -0
- package/dist/result-schema.js +78 -0
- package/dist/result-schema.js.map +1 -0
- package/dist/testing/index.d.ts +2 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +2 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/transports/in-memory.d.ts +19 -0
- package/dist/transports/in-memory.d.ts.map +1 -0
- package/dist/transports/in-memory.js +50 -0
- package/dist/transports/in-memory.js.map +1 -0
- package/dist/transports/lines.d.ts +34 -0
- package/dist/transports/lines.d.ts.map +1 -0
- package/dist/transports/lines.js +85 -0
- package/dist/transports/lines.js.map +1 -0
- package/dist/transports/pipe.d.ts +49 -0
- package/dist/transports/pipe.d.ts.map +1 -0
- package/dist/transports/pipe.js +96 -0
- package/dist/transports/pipe.js.map +1 -0
- package/dist/transports/stdio.d.ts +29 -0
- package/dist/transports/stdio.d.ts.map +1 -0
- package/dist/transports/stdio.js +170 -0
- package/dist/transports/stdio.js.map +1 -0
- package/dist/transports/streamable-http.d.ts +103 -0
- package/dist/transports/streamable-http.d.ts.map +1 -0
- package/dist/transports/streamable-http.js +531 -0
- package/dist/transports/streamable-http.js.map +1 -0
- package/dist/transports/transport.d.ts +36 -0
- package/dist/transports/transport.d.ts.map +1 -0
- package/dist/transports/transport.js +38 -0
- package/dist/transports/transport.js.map +1 -0
- package/package.json +59 -5
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type { BlobResourceContents, ContentAnnotations, TextResourceContents } from "./content.ts";
|
|
2
|
+
import type { JsonRpcId } from "./jsonrpc.ts";
|
|
3
|
+
export declare const LATEST_PROTOCOL_VERSION = "2025-11-25";
|
|
4
|
+
/**
|
|
5
|
+
* Versions the client accepts from a server. Servers that do not support the requested version answer
|
|
6
|
+
* with their own latest one, so older versions stay accepted for servers built on older SDKs.
|
|
7
|
+
*/
|
|
8
|
+
export declare const SUPPORTED_PROTOCOL_VERSIONS: readonly ["2025-11-25", "2025-06-18", "2025-03-26", "2024-11-05"];
|
|
9
|
+
export type SupportedProtocolVersion = (typeof SUPPORTED_PROTOCOL_VERSIONS)[number];
|
|
10
|
+
export interface Implementation {
|
|
11
|
+
name: string;
|
|
12
|
+
version: string;
|
|
13
|
+
title?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface Root {
|
|
16
|
+
uri: string;
|
|
17
|
+
name?: string;
|
|
18
|
+
}
|
|
19
|
+
export interface ClientCapabilities {
|
|
20
|
+
experimental?: Record<string, unknown>;
|
|
21
|
+
roots?: {
|
|
22
|
+
listChanged?: boolean;
|
|
23
|
+
};
|
|
24
|
+
sampling?: Record<string, unknown>;
|
|
25
|
+
elicitation?: Record<string, unknown>;
|
|
26
|
+
}
|
|
27
|
+
export interface ServerCapabilities {
|
|
28
|
+
experimental?: Record<string, unknown>;
|
|
29
|
+
logging?: Record<string, unknown>;
|
|
30
|
+
prompts?: {
|
|
31
|
+
listChanged?: boolean;
|
|
32
|
+
};
|
|
33
|
+
resources?: {
|
|
34
|
+
subscribe?: boolean;
|
|
35
|
+
listChanged?: boolean;
|
|
36
|
+
};
|
|
37
|
+
tools?: {
|
|
38
|
+
listChanged?: boolean;
|
|
39
|
+
};
|
|
40
|
+
completions?: Record<string, unknown>;
|
|
41
|
+
}
|
|
42
|
+
export interface InitializeParams {
|
|
43
|
+
protocolVersion: string;
|
|
44
|
+
capabilities: ClientCapabilities;
|
|
45
|
+
clientInfo: Implementation;
|
|
46
|
+
}
|
|
47
|
+
export interface InitializeResult {
|
|
48
|
+
protocolVersion: string;
|
|
49
|
+
capabilities: ServerCapabilities;
|
|
50
|
+
serverInfo: Implementation;
|
|
51
|
+
instructions?: string;
|
|
52
|
+
}
|
|
53
|
+
export interface ProgressNotification {
|
|
54
|
+
progressToken: string | number;
|
|
55
|
+
progress: number;
|
|
56
|
+
total?: number;
|
|
57
|
+
message?: string;
|
|
58
|
+
}
|
|
59
|
+
export interface CancelledNotification {
|
|
60
|
+
requestId: JsonRpcId;
|
|
61
|
+
reason?: string;
|
|
62
|
+
}
|
|
63
|
+
export interface ToolAnnotations {
|
|
64
|
+
title?: string;
|
|
65
|
+
readOnlyHint?: boolean;
|
|
66
|
+
destructiveHint?: boolean;
|
|
67
|
+
idempotentHint?: boolean;
|
|
68
|
+
openWorldHint?: boolean;
|
|
69
|
+
}
|
|
70
|
+
export interface ToolExecution {
|
|
71
|
+
taskSupport?: "forbidden" | "optional" | "required";
|
|
72
|
+
}
|
|
73
|
+
export interface Tool {
|
|
74
|
+
name: string;
|
|
75
|
+
title?: string;
|
|
76
|
+
description?: string;
|
|
77
|
+
inputSchema: Record<string, unknown>;
|
|
78
|
+
outputSchema?: Record<string, unknown>;
|
|
79
|
+
annotations?: ToolAnnotations;
|
|
80
|
+
execution?: ToolExecution;
|
|
81
|
+
_meta?: Record<string, unknown>;
|
|
82
|
+
}
|
|
83
|
+
export interface ListToolsResult {
|
|
84
|
+
tools: Tool[];
|
|
85
|
+
nextCursor?: string;
|
|
86
|
+
_meta?: Record<string, unknown>;
|
|
87
|
+
}
|
|
88
|
+
/** A resource a server lists in `resources/list`. */
|
|
89
|
+
export interface Resource {
|
|
90
|
+
uri: string;
|
|
91
|
+
name: string;
|
|
92
|
+
title?: string;
|
|
93
|
+
description?: string;
|
|
94
|
+
mimeType?: string;
|
|
95
|
+
size?: number;
|
|
96
|
+
annotations?: ContentAnnotations;
|
|
97
|
+
_meta?: Record<string, unknown>;
|
|
98
|
+
}
|
|
99
|
+
/** A family of resources, addressed by an RFC 6570 URI template, from `resources/templates/list`. */
|
|
100
|
+
export interface ResourceTemplate {
|
|
101
|
+
uriTemplate: string;
|
|
102
|
+
name: string;
|
|
103
|
+
title?: string;
|
|
104
|
+
description?: string;
|
|
105
|
+
mimeType?: string;
|
|
106
|
+
annotations?: ContentAnnotations;
|
|
107
|
+
_meta?: Record<string, unknown>;
|
|
108
|
+
}
|
|
109
|
+
export interface ListResourcesResult {
|
|
110
|
+
resources: Resource[];
|
|
111
|
+
nextCursor?: string;
|
|
112
|
+
_meta?: Record<string, unknown>;
|
|
113
|
+
}
|
|
114
|
+
export interface ListResourceTemplatesResult {
|
|
115
|
+
resourceTemplates: ResourceTemplate[];
|
|
116
|
+
nextCursor?: string;
|
|
117
|
+
_meta?: Record<string, unknown>;
|
|
118
|
+
}
|
|
119
|
+
export interface ReadResourceResult {
|
|
120
|
+
contents: (TextResourceContents | BlobResourceContents)[];
|
|
121
|
+
_meta?: Record<string, unknown>;
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/protocol/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACnG,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C,eAAO,MAAM,uBAAuB,eAAe,CAAC;AACpD;;;GAGG;AACH,eAAO,MAAM,2BAA2B,0BAA6B,YAAY,EAAE,YAAY,EAAE,YAAY,CAAU,CAAC;AACxH,MAAM,MAAM,wBAAwB,GAAG,CAAC,OAAO,2BAA2B,CAAC,CAAC,MAAM,CAAC,CAAC;AAEpF,MAAM,WAAW,cAAc;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,IAAI;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,kBAAkB;IAClC,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACvC,KAAK,CAAC,EAAE;QAAE,WAAW,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC;IA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C;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,kBAAkB,CAAC;IACjC,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC;AAED,MAAM,WAAW,mBAAmB;IACnC,SAAS,EAAE,QAAQ,EAAE,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC;AAED,MAAM,WAAW,2BAA2B;IAC3C,iBAAiB,EAAE,gBAAgB,EAAE,CAAC;IACtC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC;AAED,MAAM,WAAW,kBAAkB;IAClC,QAAQ,EAAE,CAAC,oBAAoB,GAAG,oBAAoB,CAAC,EAAE,CAAC;IAC1D,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export const LATEST_PROTOCOL_VERSION = "2025-11-25";
|
|
2
|
+
/**
|
|
3
|
+
* Versions the client accepts from a server. Servers that do not support the requested version answer
|
|
4
|
+
* with their own latest one, so older versions stay accepted for servers built on older SDKs.
|
|
5
|
+
*/
|
|
6
|
+
export const SUPPORTED_PROTOCOL_VERSIONS = [LATEST_PROTOCOL_VERSION, "2025-06-18", "2025-03-26", "2024-11-05"];
|
|
7
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/protocol/types.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,MAAM,uBAAuB,GAAG,YAAY,CAAC;AACpD;;;GAGG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,CAAC,uBAAuB,EAAE,YAAY,EAAE,YAAY,EAAE,YAAY,CAAU,CAAC","sourcesContent":["import type { BlobResourceContents, ContentAnnotations, TextResourceContents } from \"./content.ts\";\nimport type { JsonRpcId } from \"./jsonrpc.ts\";\n\nexport const LATEST_PROTOCOL_VERSION = \"2025-11-25\";\n/**\n * Versions the client accepts from a server. Servers that do not support the requested version answer\n * with their own latest one, so older versions stay accepted for servers built on older SDKs.\n */\nexport const SUPPORTED_PROTOCOL_VERSIONS = [LATEST_PROTOCOL_VERSION, \"2025-06-18\", \"2025-03-26\", \"2024-11-05\"] as const;\nexport type SupportedProtocolVersion = (typeof SUPPORTED_PROTOCOL_VERSIONS)[number];\n\nexport interface Implementation {\n\tname: string;\n\tversion: string;\n\ttitle?: string;\n}\n\nexport interface Root {\n\turi: string;\n\tname?: string;\n}\n\nexport interface ClientCapabilities {\n\texperimental?: Record<string, unknown>;\n\troots?: { listChanged?: boolean };\n\tsampling?: Record<string, unknown>;\n\telicitation?: Record<string, unknown>;\n}\n\nexport interface ServerCapabilities {\n\texperimental?: Record<string, unknown>;\n\tlogging?: Record<string, unknown>;\n\tprompts?: { listChanged?: boolean };\n\tresources?: { subscribe?: boolean; listChanged?: boolean };\n\ttools?: { listChanged?: boolean };\n\tcompletions?: Record<string, unknown>;\n}\n\nexport interface InitializeParams {\n\tprotocolVersion: string;\n\tcapabilities: ClientCapabilities;\n\tclientInfo: Implementation;\n}\n\nexport interface InitializeResult {\n\tprotocolVersion: string;\n\tcapabilities: ServerCapabilities;\n\tserverInfo: Implementation;\n\tinstructions?: string;\n}\n\nexport interface ProgressNotification {\n\tprogressToken: string | number;\n\tprogress: number;\n\ttotal?: number;\n\tmessage?: string;\n}\n\nexport interface CancelledNotification {\n\trequestId: JsonRpcId;\n\treason?: string;\n}\n\nexport interface ToolAnnotations {\n\ttitle?: string;\n\treadOnlyHint?: boolean;\n\tdestructiveHint?: boolean;\n\tidempotentHint?: boolean;\n\topenWorldHint?: boolean;\n}\n\nexport interface ToolExecution {\n\ttaskSupport?: \"forbidden\" | \"optional\" | \"required\";\n}\n\nexport interface Tool {\n\tname: string;\n\ttitle?: string;\n\tdescription?: string;\n\tinputSchema: Record<string, unknown>;\n\toutputSchema?: Record<string, unknown>;\n\tannotations?: ToolAnnotations;\n\texecution?: ToolExecution;\n\t_meta?: Record<string, unknown>;\n}\n\nexport interface ListToolsResult {\n\ttools: Tool[];\n\tnextCursor?: string;\n\t_meta?: Record<string, unknown>;\n}\n\n/** A resource a server lists in `resources/list`. */\nexport interface Resource {\n\turi: string;\n\tname: string;\n\ttitle?: string;\n\tdescription?: string;\n\tmimeType?: string;\n\tsize?: number;\n\tannotations?: ContentAnnotations;\n\t_meta?: Record<string, unknown>;\n}\n\n/** A family of resources, addressed by an RFC 6570 URI template, from `resources/templates/list`. */\nexport interface ResourceTemplate {\n\turiTemplate: string;\n\tname: string;\n\ttitle?: string;\n\tdescription?: string;\n\tmimeType?: string;\n\tannotations?: ContentAnnotations;\n\t_meta?: Record<string, unknown>;\n}\n\nexport interface ListResourcesResult {\n\tresources: Resource[];\n\tnextCursor?: string;\n\t_meta?: Record<string, unknown>;\n}\n\nexport interface ListResourceTemplatesResult {\n\tresourceTemplates: ResourceTemplate[];\n\tnextCursor?: string;\n\t_meta?: Record<string, unknown>;\n}\n\nexport interface ReadResourceResult {\n\tcontents: (TextResourceContents | BlobResourceContents)[];\n\t_meta?: Record<string, unknown>;\n}\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The output schema of an MCP tool for programs that call it: the whole `CallToolResult`, with the tool's own output
|
|
3
|
+
* schema as `structuredContent`.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Output schema of an MCP tool: the `CallToolResult` programs receive (without `_meta`, which callers drop), with the
|
|
7
|
+
* tool's own output schema as `structuredContent`. Without one, structured content may be any JSON value.
|
|
8
|
+
*/
|
|
9
|
+
export declare function createMcpResultSchema(structuredContentSchema: Record<string, unknown> | undefined): Record<string, unknown>;
|
|
10
|
+
//# sourceMappingURL=result-schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result-schema.d.ts","sourceRoot":"","sources":["../src/result-schema.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAkBH;;;GAGG;AACH,wBAAgB,qBAAqB,CACpC,uBAAuB,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,GAC1D,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAczB"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The output schema of an MCP tool for programs that call it: the whole `CallToolResult`, with the tool's own output
|
|
3
|
+
* schema as `structuredContent`.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* MCP content blocks as one compact type with the fields of text and image blocks, which scripts
|
|
7
|
+
* read most. Rendered into every MCP tool's declaration.
|
|
8
|
+
*/
|
|
9
|
+
const CONTENT_BLOCK_SCHEMA = {
|
|
10
|
+
type: "object",
|
|
11
|
+
properties: {
|
|
12
|
+
type: { enum: ["text", "image", "audio", "resource_link", "resource"] },
|
|
13
|
+
text: { type: "string" },
|
|
14
|
+
data: { type: "string" },
|
|
15
|
+
mimeType: { type: "string" },
|
|
16
|
+
},
|
|
17
|
+
required: ["type"],
|
|
18
|
+
additionalProperties: true,
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Output schema of an MCP tool: the `CallToolResult` programs receive (without `_meta`, which callers drop), with the
|
|
22
|
+
* tool's own output schema as `structuredContent`. Without one, structured content may be any JSON value.
|
|
23
|
+
*/
|
|
24
|
+
export function createMcpResultSchema(structuredContentSchema) {
|
|
25
|
+
return {
|
|
26
|
+
type: "object",
|
|
27
|
+
properties: {
|
|
28
|
+
content: { type: "array", items: CONTENT_BLOCK_SCHEMA },
|
|
29
|
+
structuredContent: structuredContentSchema
|
|
30
|
+
? relocateSchema(structuredContentSchema, "#/properties/structuredContent")
|
|
31
|
+
: {},
|
|
32
|
+
isError: { type: "boolean" },
|
|
33
|
+
},
|
|
34
|
+
required: ["content"],
|
|
35
|
+
// Fields newer protocol versions add.
|
|
36
|
+
additionalProperties: true,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** Keywords whose values are data, so a `$ref` key in them is not a reference. */
|
|
40
|
+
const SCHEMA_DATA_KEYWORDS = new Set(["const", "enum", "default", "examples"]);
|
|
41
|
+
/** Keywords whose values map names to schemas, so their keys are names, not keywords. */
|
|
42
|
+
const SCHEMA_MAP_KEYWORDS = new Set([
|
|
43
|
+
"properties",
|
|
44
|
+
"patternProperties",
|
|
45
|
+
"dependentSchemas",
|
|
46
|
+
"dependencies",
|
|
47
|
+
"$defs",
|
|
48
|
+
"definitions",
|
|
49
|
+
]);
|
|
50
|
+
/**
|
|
51
|
+
* `schema` as a subschema at the JSON pointer `pointer` (`#/properties/structuredContent`) of
|
|
52
|
+
* another schema: its local references (`#`, `#/$defs/Item`) are rewritten to point below
|
|
53
|
+
* `pointer`, so they resolve to the same subschemas as before.
|
|
54
|
+
*/
|
|
55
|
+
function relocateSchema(schema, pointer) {
|
|
56
|
+
// The server's schema is wire input of any JSON shape.
|
|
57
|
+
if (Array.isArray(schema))
|
|
58
|
+
return schema.map((item) => relocateSchema(item, pointer));
|
|
59
|
+
if (typeof schema !== "object" || schema === null)
|
|
60
|
+
return schema;
|
|
61
|
+
const relocated = {};
|
|
62
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
63
|
+
if (key === "$ref" && typeof value === "string" && (value === "#" || value.startsWith("#/"))) {
|
|
64
|
+
relocated[key] = `${pointer}${value.slice(1)}`;
|
|
65
|
+
}
|
|
66
|
+
else if (SCHEMA_DATA_KEYWORDS.has(key)) {
|
|
67
|
+
relocated[key] = value;
|
|
68
|
+
}
|
|
69
|
+
else if (SCHEMA_MAP_KEYWORDS.has(key) && typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
70
|
+
relocated[key] = Object.fromEntries(Object.entries(value).map(([name, subschema]) => [name, relocateSchema(subschema, pointer)]));
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
relocated[key] = relocateSchema(value, pointer);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return relocated;
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=result-schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result-schema.js","sourceRoot":"","sources":["../src/result-schema.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;GAGG;AACH,MAAM,oBAAoB,GAAG;IAC5B,IAAI,EAAE,QAAQ;IACd,UAAU,EAAE;QACX,IAAI,EAAE,EAAE,IAAI,EAAE,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,UAAU,CAAC,EAAE;QACvE,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QACxB,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QACxB,QAAQ,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;KAC5B;IACD,QAAQ,EAAE,CAAC,MAAM,CAAC;IAClB,oBAAoB,EAAE,IAAI;CAC1B,CAAC;AAEF;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CACpC,uBAA4D;IAE5D,OAAO;QACN,IAAI,EAAE,QAAQ;QACd,UAAU,EAAE;YACX,OAAO,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,oBAAoB,EAAE;YACvD,iBAAiB,EAAE,uBAAuB;gBACzC,CAAC,CAAC,cAAc,CAAC,uBAAuB,EAAE,gCAAgC,CAAC;gBAC3E,CAAC,CAAC,EAAE;YACL,OAAO,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;SAC5B;QACD,QAAQ,EAAE,CAAC,SAAS,CAAC;QACrB,sCAAsC;QACt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OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;YACjH,SAAS,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,WAAW,CAClC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,SAAS,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,cAAc,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC,CAC5F,CAAC;QACH,CAAC;aAAM,CAAC;YACP,SAAS,CAAC,GAAG,CAAC,GAAG,cAAc,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACjD,CAAC;IACF,CAAC;IACD,OAAO,SAAS,CAAC;AAClB,CAAC","sourcesContent":["/**\n * The output schema of an MCP tool for programs that call it: the whole `CallToolResult`, with the tool's own output\n * schema as `structuredContent`.\n */\n\n/**\n * MCP content blocks as one compact type with the fields of text and image blocks, which scripts\n * read most. Rendered into every MCP tool's declaration.\n */\nconst CONTENT_BLOCK_SCHEMA = {\n\ttype: \"object\",\n\tproperties: {\n\t\ttype: { enum: [\"text\", \"image\", \"audio\", \"resource_link\", \"resource\"] },\n\t\ttext: { type: \"string\" },\n\t\tdata: { type: \"string\" },\n\t\tmimeType: { type: \"string\" },\n\t},\n\trequired: [\"type\"],\n\tadditionalProperties: true,\n};\n\n/**\n * Output schema of an MCP tool: the `CallToolResult` programs receive (without `_meta`, which callers drop), with the\n * tool's own output schema as `structuredContent`. Without one, structured content may be any JSON value.\n */\nexport function createMcpResultSchema(\n\tstructuredContentSchema: Record<string, unknown> | undefined,\n): Record<string, unknown> {\n\treturn {\n\t\ttype: \"object\",\n\t\tproperties: {\n\t\t\tcontent: { type: \"array\", items: CONTENT_BLOCK_SCHEMA },\n\t\t\tstructuredContent: structuredContentSchema\n\t\t\t\t? relocateSchema(structuredContentSchema, \"#/properties/structuredContent\")\n\t\t\t\t: {},\n\t\t\tisError: { type: \"boolean\" },\n\t\t},\n\t\trequired: [\"content\"],\n\t\t// Fields newer protocol versions add.\n\t\tadditionalProperties: true,\n\t};\n}\n\n/** Keywords whose values are data, so a `$ref` key in them is not a reference. */\nconst SCHEMA_DATA_KEYWORDS: ReadonlySet<string> = new Set([\"const\", \"enum\", \"default\", \"examples\"]);\n\n/** Keywords whose values map names to schemas, so their keys are names, not keywords. */\nconst SCHEMA_MAP_KEYWORDS: ReadonlySet<string> = new Set([\n\t\"properties\",\n\t\"patternProperties\",\n\t\"dependentSchemas\",\n\t\"dependencies\",\n\t\"$defs\",\n\t\"definitions\",\n]);\n\n/**\n * `schema` as a subschema at the JSON pointer `pointer` (`#/properties/structuredContent`) of\n * another schema: its local references (`#`, `#/$defs/Item`) are rewritten to point below\n * `pointer`, so they resolve to the same subschemas as before.\n */\nfunction relocateSchema(schema: unknown, pointer: string): unknown {\n\t// The server's schema is wire input of any JSON shape.\n\tif (Array.isArray(schema)) return schema.map((item) => relocateSchema(item, pointer));\n\tif (typeof schema !== \"object\" || schema === null) return schema;\n\tconst relocated: Record<string, unknown> = {};\n\tfor (const [key, value] of Object.entries(schema)) {\n\t\tif (key === \"$ref\" && typeof value === \"string\" && (value === \"#\" || value.startsWith(\"#/\"))) {\n\t\t\trelocated[key] = `${pointer}${value.slice(1)}`;\n\t\t} else if (SCHEMA_DATA_KEYWORDS.has(key)) {\n\t\t\trelocated[key] = value;\n\t\t} else if (SCHEMA_MAP_KEYWORDS.has(key) && typeof value === \"object\" && value !== null && !Array.isArray(value)) {\n\t\t\trelocated[key] = Object.fromEntries(\n\t\t\t\tObject.entries(value).map(([name, subschema]) => [name, relocateSchema(subschema, pointer)]),\n\t\t\t);\n\t\t} else {\n\t\t\trelocated[key] = relocateSchema(value, pointer);\n\t\t}\n\t}\n\treturn relocated;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC","sourcesContent":["export { createInMemoryTransportPair, InMemoryTransport } from \"../transports/in-memory.ts\";\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type JsonRpcMessage } from "../protocol/jsonrpc.ts";
|
|
2
|
+
import { type McpTransport, TransportEvents } from "./transport.ts";
|
|
3
|
+
export declare class InMemoryTransport extends TransportEvents implements McpTransport {
|
|
4
|
+
private peer;
|
|
5
|
+
private started;
|
|
6
|
+
private closed;
|
|
7
|
+
connectPeer(peer: InMemoryTransport): void;
|
|
8
|
+
start(): Promise<void>;
|
|
9
|
+
send(message: JsonRpcMessage): Promise<void>;
|
|
10
|
+
close(): Promise<void>;
|
|
11
|
+
/** Exposed so tests can simulate transport-level failures. */
|
|
12
|
+
emitError(error: unknown): void;
|
|
13
|
+
private deliver;
|
|
14
|
+
}
|
|
15
|
+
export declare function createInMemoryTransportPair(): {
|
|
16
|
+
client: InMemoryTransport;
|
|
17
|
+
server: InMemoryTransport;
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=in-memory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"in-memory.d.ts","sourceRoot":"","sources":["../../src/transports/in-memory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAA4B,MAAM,wBAAwB,CAAC;AACvF,OAAO,EAAE,KAAK,YAAY,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEpE,qBAAa,iBAAkB,SAAQ,eAAgB,YAAW,YAAY;IAC7E,OAAO,CAAC,IAAI,CAAgC;IAC5C,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,MAAM,CAAS;IAEvB,WAAW,CAAC,IAAI,EAAE,iBAAiB,GAAG,IAAI,CAGzC;IAEK,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAG3B;IAEK,IAAI,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAMjD;IAEK,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAK3B;IAED,8DAA8D;IACrD,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAEvC;IAED,OAAO,CAAC,OAAO;CAIf;AAED,wBAAgB,2BAA2B,IAAI;IAAE,MAAM,EAAE,iBAAiB,CAAC;IAAC,MAAM,EAAE,iBAAiB,CAAA;CAAE,CAMtG"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { McpConnectionClosedError } from "../protocol/jsonrpc.js";
|
|
2
|
+
import { TransportEvents } from "./transport.js";
|
|
3
|
+
export class InMemoryTransport extends TransportEvents {
|
|
4
|
+
peer;
|
|
5
|
+
started = false;
|
|
6
|
+
closed = false;
|
|
7
|
+
connectPeer(peer) {
|
|
8
|
+
if (this.peer)
|
|
9
|
+
throw new Error("In-memory MCP transport already has a peer");
|
|
10
|
+
this.peer = peer;
|
|
11
|
+
}
|
|
12
|
+
async start() {
|
|
13
|
+
if (this.closed)
|
|
14
|
+
throw new McpConnectionClosedError();
|
|
15
|
+
this.started = true;
|
|
16
|
+
}
|
|
17
|
+
async send(message) {
|
|
18
|
+
if (!this.started || this.closed)
|
|
19
|
+
throw new McpConnectionClosedError();
|
|
20
|
+
const peer = this.peer;
|
|
21
|
+
if (!peer?.started || peer.closed)
|
|
22
|
+
throw new McpConnectionClosedError("In-memory MCP peer is not connected");
|
|
23
|
+
const copy = structuredClone(message);
|
|
24
|
+
queueMicrotask(() => peer.deliver(copy));
|
|
25
|
+
}
|
|
26
|
+
async close() {
|
|
27
|
+
if (this.closed)
|
|
28
|
+
return;
|
|
29
|
+
this.closed = true;
|
|
30
|
+
this.emitClose();
|
|
31
|
+
await this.peer?.close();
|
|
32
|
+
}
|
|
33
|
+
/** Exposed so tests can simulate transport-level failures. */
|
|
34
|
+
emitError(error) {
|
|
35
|
+
super.emitError(error);
|
|
36
|
+
}
|
|
37
|
+
deliver(message) {
|
|
38
|
+
if (this.closed)
|
|
39
|
+
return;
|
|
40
|
+
this.emitMessage(message);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export function createInMemoryTransportPair() {
|
|
44
|
+
const client = new InMemoryTransport();
|
|
45
|
+
const server = new InMemoryTransport();
|
|
46
|
+
client.connectPeer(server);
|
|
47
|
+
server.connectPeer(client);
|
|
48
|
+
return { client, server };
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=in-memory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"in-memory.js","sourceRoot":"","sources":["../../src/transports/in-memory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AACvF,OAAO,EAAqB,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEpE,MAAM,OAAO,iBAAkB,SAAQ,eAAe;IAC7C,IAAI,CAAgC;IACpC,OAAO,GAAG,KAAK,CAAC;IAChB,MAAM,GAAG,KAAK,CAAC;IAEvB,WAAW,CAAC,IAAuB;QAClC,IAAI,IAAI,CAAC,IAAI;YAAE,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;QAC7E,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IAClB,CAAC;IAED,KAAK,CAAC,KAAK;QACV,IAAI,IAAI,CAAC,MAAM;YAAE,MAAM,IAAI,wBAAwB,EAAE,CAAC;QACtD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;IACrB,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,OAAuB;QACjC,IAAI,CAAC,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,MAAM;YAAE,MAAM,IAAI,wBAAwB,EAAE,CAAC;QACvE,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACvB,IAAI,CAAC,IAAI,EAAE,OAAO,IAAI,IAAI,CAAC,MAAM;YAAE,MAAM,IAAI,wBAAwB,CAAC,qCAAqC,CAAC,CAAC;QAC7G,MAAM,IAAI,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC;QACtC,cAAc,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;IAC1C,CAAC;IAED,KAAK,CAAC,KAAK;QACV,IAAI,IAAI,CAAC,MAAM;YAAE,OAAO;QACxB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACnB,IAAI,CAAC,SAAS,EAAE,CAAC;QACjB,MAAM,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC;IAC1B,CAAC;IAED,8DAA8D;IACrD,SAAS,CAAC,KAAc;QAChC,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IACxB,CAAC;IAEO,OAAO,CAAC,OAAuB;QACtC,IAAI,IAAI,CAAC,MAAM;YAAE,OAAO;QACxB,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IAC3B,CAAC;CACD;AAED,MAAM,UAAU,2BAA2B;IAC1C,MAAM,MAAM,GAAG,IAAI,iBAAiB,EAAE,CAAC;IACvC,MAAM,MAAM,GAAG,IAAI,iBAAiB,EAAE,CAAC;IACvC,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IAC3B,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IAC3B,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;AAC3B,CAAC","sourcesContent":["import { type JsonRpcMessage, McpConnectionClosedError } from \"../protocol/jsonrpc.ts\";\nimport { type McpTransport, TransportEvents } from \"./transport.ts\";\n\nexport class InMemoryTransport extends TransportEvents implements McpTransport {\n\tprivate peer: InMemoryTransport | undefined;\n\tprivate started = false;\n\tprivate closed = false;\n\n\tconnectPeer(peer: InMemoryTransport): void {\n\t\tif (this.peer) throw new Error(\"In-memory MCP transport already has a peer\");\n\t\tthis.peer = peer;\n\t}\n\n\tasync start(): Promise<void> {\n\t\tif (this.closed) throw new McpConnectionClosedError();\n\t\tthis.started = true;\n\t}\n\n\tasync send(message: JsonRpcMessage): Promise<void> {\n\t\tif (!this.started || this.closed) throw new McpConnectionClosedError();\n\t\tconst peer = this.peer;\n\t\tif (!peer?.started || peer.closed) throw new McpConnectionClosedError(\"In-memory MCP peer is not connected\");\n\t\tconst copy = structuredClone(message);\n\t\tqueueMicrotask(() => peer.deliver(copy));\n\t}\n\n\tasync close(): Promise<void> {\n\t\tif (this.closed) return;\n\t\tthis.closed = true;\n\t\tthis.emitClose();\n\t\tawait this.peer?.close();\n\t}\n\n\t/** Exposed so tests can simulate transport-level failures. */\n\toverride emitError(error: unknown): void {\n\t\tsuper.emitError(error);\n\t}\n\n\tprivate deliver(message: JsonRpcMessage): void {\n\t\tif (this.closed) return;\n\t\tthis.emitMessage(message);\n\t}\n}\n\nexport function createInMemoryTransportPair(): { client: InMemoryTransport; server: InMemoryTransport } {\n\tconst client = new InMemoryTransport();\n\tconst server = new InMemoryTransport();\n\tclient.connectPeer(server);\n\tserver.connectPeer(client);\n\treturn { client, server };\n}\n"]}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type JsonRpcMessage } from "../protocol/jsonrpc.ts";
|
|
2
|
+
export interface JsonRpcLineReaderOptions {
|
|
3
|
+
maxMessageBytes?: number;
|
|
4
|
+
maxStderrBytes?: number;
|
|
5
|
+
/** Every stderr chunk, decoded as UTF-8. */
|
|
6
|
+
onStderr?: (chunk: string) => void;
|
|
7
|
+
onMessage(message: JsonRpcMessage): void;
|
|
8
|
+
onError(error: unknown): void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Reads a program's output as MCP's stdio framing: newline-delimited JSON-RPC messages on stdout, and a bounded
|
|
12
|
+
* tail of stderr for diagnostics.
|
|
13
|
+
*/
|
|
14
|
+
export declare class JsonRpcLineReader {
|
|
15
|
+
private readonly options;
|
|
16
|
+
/** The bytes of the incomplete message received so far. */
|
|
17
|
+
private stdoutChunks;
|
|
18
|
+
private stdoutPendingBytes;
|
|
19
|
+
private stderrBuffer;
|
|
20
|
+
constructor(options: JsonRpcLineReaderOptions);
|
|
21
|
+
/** The last `maxStderrBytes` of stderr. */
|
|
22
|
+
get stderr(): string;
|
|
23
|
+
/**
|
|
24
|
+
* Takes the next stdout bytes and reports every complete message, or the error parsing it. Bytes of an
|
|
25
|
+
* incomplete message are kept as chunks and joined once when its newline arrives, so a large message that
|
|
26
|
+
* arrives in many chunks is copied and scanned only once.
|
|
27
|
+
*/
|
|
28
|
+
readStdout(chunk: Uint8Array): void;
|
|
29
|
+
/** Takes the next stderr bytes into the tail and passes them to `onStderr`. */
|
|
30
|
+
readStderr(chunk: Uint8Array): void;
|
|
31
|
+
/** Ends stdout: a message without its newline is reported as an error and dropped. */
|
|
32
|
+
end(): void;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=lines.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lines.d.ts","sourceRoot":"","sources":["../../src/transports/lines.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAuB,MAAM,wBAAwB,CAAC;AAKlF,MAAM,WAAW,wBAAwB;IACxC,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,4CAA4C;IAC5C,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACnC,SAAS,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI,CAAC;IACzC,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAAC;CAC9B;AAED;;;GAGG;AACH,qBAAa,iBAAiB;IAC7B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA2B;IACnD,2DAA2D;IAC3D,OAAO,CAAC,YAAY,CAAgB;IACpC,OAAO,CAAC,kBAAkB,CAAK;IAC/B,OAAO,CAAC,YAAY,CAAmB;IAEvC,YAAY,OAAO,EAAE,wBAAwB,EAE5C;IAED,2CAA2C;IAC3C,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED;;;;OAIG;IACH,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAuClC;IAED,+EAA+E;IAC/E,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAKlC;IAED,sFAAsF;IACtF,GAAG,IAAI,IAAI,CAMV;CACD"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { parseJsonRpcMessage } from "../protocol/jsonrpc.js";
|
|
2
|
+
import { DEFAULT_MAX_MESSAGE_BYTES } from "./transport.js";
|
|
3
|
+
const DEFAULT_MAX_STDERR_BYTES = 64 * 1024;
|
|
4
|
+
/**
|
|
5
|
+
* Reads a program's output as MCP's stdio framing: newline-delimited JSON-RPC messages on stdout, and a bounded
|
|
6
|
+
* tail of stderr for diagnostics.
|
|
7
|
+
*/
|
|
8
|
+
export class JsonRpcLineReader {
|
|
9
|
+
options;
|
|
10
|
+
/** The bytes of the incomplete message received so far. */
|
|
11
|
+
stdoutChunks = [];
|
|
12
|
+
stdoutPendingBytes = 0;
|
|
13
|
+
stderrBuffer = Buffer.alloc(0);
|
|
14
|
+
constructor(options) {
|
|
15
|
+
this.options = options;
|
|
16
|
+
}
|
|
17
|
+
/** The last `maxStderrBytes` of stderr. */
|
|
18
|
+
get stderr() {
|
|
19
|
+
return this.stderrBuffer.toString("utf8");
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Takes the next stdout bytes and reports every complete message, or the error parsing it. Bytes of an
|
|
23
|
+
* incomplete message are kept as chunks and joined once when its newline arrives, so a large message that
|
|
24
|
+
* arrives in many chunks is copied and scanned only once.
|
|
25
|
+
*/
|
|
26
|
+
readStdout(chunk) {
|
|
27
|
+
const data = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength);
|
|
28
|
+
const maxMessageBytes = this.options.maxMessageBytes ?? DEFAULT_MAX_MESSAGE_BYTES;
|
|
29
|
+
let start = 0;
|
|
30
|
+
while (true) {
|
|
31
|
+
const newline = data.indexOf(0x0a, start);
|
|
32
|
+
if (newline < 0) {
|
|
33
|
+
if (start < data.length) {
|
|
34
|
+
// Copied, since the caller may reuse the chunk's memory.
|
|
35
|
+
this.stdoutChunks.push(Buffer.from(data.subarray(start)));
|
|
36
|
+
this.stdoutPendingBytes += data.length - start;
|
|
37
|
+
}
|
|
38
|
+
if (this.stdoutPendingBytes > maxMessageBytes) {
|
|
39
|
+
this.stdoutChunks = [];
|
|
40
|
+
this.stdoutPendingBytes = 0;
|
|
41
|
+
this.options.onError(new Error(`MCP stdio message exceeds ${maxMessageBytes} bytes`));
|
|
42
|
+
}
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
let line = data.subarray(start, newline);
|
|
46
|
+
if (this.stdoutChunks.length > 0) {
|
|
47
|
+
this.stdoutChunks.push(line);
|
|
48
|
+
line = Buffer.concat(this.stdoutChunks, this.stdoutPendingBytes + line.length);
|
|
49
|
+
this.stdoutChunks = [];
|
|
50
|
+
this.stdoutPendingBytes = 0;
|
|
51
|
+
}
|
|
52
|
+
start = newline + 1;
|
|
53
|
+
if (line.length > maxMessageBytes) {
|
|
54
|
+
this.options.onError(new Error(`MCP stdio message exceeds ${maxMessageBytes} bytes`));
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const text = line.toString("utf8").replace(/\r$/, "");
|
|
58
|
+
if (!text.trim())
|
|
59
|
+
continue;
|
|
60
|
+
try {
|
|
61
|
+
this.options.onMessage(parseJsonRpcMessage(JSON.parse(text)));
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
this.options.onError(error);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** Takes the next stderr bytes into the tail and passes them to `onStderr`. */
|
|
69
|
+
readStderr(chunk) {
|
|
70
|
+
const maxStderrBytes = this.options.maxStderrBytes ?? DEFAULT_MAX_STDERR_BYTES;
|
|
71
|
+
this.stderrBuffer = Buffer.concat([this.stderrBuffer, chunk]);
|
|
72
|
+
if (this.stderrBuffer.length > maxStderrBytes)
|
|
73
|
+
this.stderrBuffer = this.stderrBuffer.subarray(-maxStderrBytes);
|
|
74
|
+
this.options.onStderr?.(Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength).toString("utf8"));
|
|
75
|
+
}
|
|
76
|
+
/** Ends stdout: a message without its newline is reported as an error and dropped. */
|
|
77
|
+
end() {
|
|
78
|
+
if (Buffer.concat(this.stdoutChunks).toString("utf8").trim()) {
|
|
79
|
+
this.options.onError(new Error("MCP stdio server closed with an incomplete JSON-RPC message"));
|
|
80
|
+
}
|
|
81
|
+
this.stdoutChunks = [];
|
|
82
|
+
this.stdoutPendingBytes = 0;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=lines.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lines.js","sourceRoot":"","sources":["../../src/transports/lines.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAClF,OAAO,EAAE,yBAAyB,EAAE,MAAM,gBAAgB,CAAC;AAE3D,MAAM,wBAAwB,GAAG,EAAE,GAAG,IAAI,CAAC;AAW3C;;;GAGG;AACH,MAAM,OAAO,iBAAiB;IACZ,OAAO,CAA2B;IACnD,2DAA2D;IACnD,YAAY,GAAa,EAAE,CAAC;IAC5B,kBAAkB,GAAG,CAAC,CAAC;IACvB,YAAY,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAEvC,YAAY,OAAiC;QAC5C,IAAI,CAAC,OAAO,GAAG,OAAO,C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CAAC,MAAM,GAAG,cAAc;YAAE,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,cAAc,CAAC,CAAC;QAC/G,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC;IACzG,CAAC;IAED,sFAAsF;IACtF,GAAG;QACF,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;YAC9D,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,KAAK,CAAC,6DAA6D,CAAC,CAAC,CAAC;QAChG,CAAC;QACD,IAAI,CAAC,YAAY,GAAG,EAAE,CAAC;QACvB,IAAI,CAAC,kBAAkB,GAAG,CAAC,CAAC;IAC7B,CAAC;CACD","sourcesContent":["import { type JsonRpcMessage, parseJsonRpcMessage } from \"../protocol/jsonrpc.ts\";\nimport { DEFAULT_MAX_MESSAGE_BYTES } from \"./transport.ts\";\n\nconst DEFAULT_MAX_STDERR_BYTES = 64 * 1024;\n\nexport interface JsonRpcLineReaderOptions {\n\tmaxMessageBytes?: number;\n\tmaxStderrBytes?: number;\n\t/** Every stderr chunk, decoded as UTF-8. */\n\tonStderr?: (chunk: string) => void;\n\tonMessage(message: JsonRpcMessage): void;\n\tonError(error: unknown): void;\n}\n\n/**\n * Reads a program's output as MCP's stdio framing: newline-delimited JSON-RPC messages on stdout, and a bounded\n * tail of stderr for diagnostics.\n */\nexport class JsonRpcLineReader {\n\tprivate readonly options: JsonRpcLineReaderOptions;\n\t/** The bytes of the incomplete message received so far. */\n\tprivate stdoutChunks: Buffer[] = [];\n\tprivate stdoutPendingBytes = 0;\n\tprivate stderrBuffer = Buffer.alloc(0);\n\n\tconstructor(options: JsonRpcLineReaderOptions) {\n\t\tthis.options = options;\n\t}\n\n\t/** The last `maxStderrBytes` of stderr. */\n\tget stderr(): string {\n\t\treturn this.stderrBuffer.toString(\"utf8\");\n\t}\n\n\t/**\n\t * Takes the next stdout bytes and reports every complete message, or the error parsing it. Bytes of an\n\t * incomplete message are kept as chunks and joined once when its newline arrives, so a large message that\n\t * arrives in many chunks is copied and scanned only once.\n\t */\n\treadStdout(chunk: Uint8Array): void {\n\t\tconst data = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength);\n\t\tconst maxMessageBytes = this.options.maxMessageBytes ?? DEFAULT_MAX_MESSAGE_BYTES;\n\t\tlet start = 0;\n\t\twhile (true) {\n\t\t\tconst newline = data.indexOf(0x0a, start);\n\t\t\tif (newline < 0) {\n\t\t\t\tif (start < data.length) {\n\t\t\t\t\t// Copied, since the caller may reuse the chunk's memory.\n\t\t\t\t\tthis.stdoutChunks.push(Buffer.from(data.subarray(start)));\n\t\t\t\t\tthis.stdoutPendingBytes += data.length - start;\n\t\t\t\t}\n\t\t\t\tif (this.stdoutPendingBytes > maxMessageBytes) {\n\t\t\t\t\tthis.stdoutChunks = [];\n\t\t\t\t\tthis.stdoutPendingBytes = 0;\n\t\t\t\t\tthis.options.onError(new Error(`MCP stdio message exceeds ${maxMessageBytes} bytes`));\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tlet line = data.subarray(start, newline);\n\t\t\tif (this.stdoutChunks.length > 0) {\n\t\t\t\tthis.stdoutChunks.push(line);\n\t\t\t\tline = Buffer.concat(this.stdoutChunks, this.stdoutPendingBytes + line.length);\n\t\t\t\tthis.stdoutChunks = [];\n\t\t\t\tthis.stdoutPendingBytes = 0;\n\t\t\t}\n\t\t\tstart = newline + 1;\n\t\t\tif (line.length > maxMessageBytes) {\n\t\t\t\tthis.options.onError(new Error(`MCP stdio message exceeds ${maxMessageBytes} bytes`));\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst text = line.toString(\"utf8\").replace(/\\r$/, \"\");\n\t\t\tif (!text.trim()) continue;\n\t\t\ttry {\n\t\t\t\tthis.options.onMessage(parseJsonRpcMessage(JSON.parse(text)));\n\t\t\t} catch (error) {\n\t\t\t\tthis.options.onError(error);\n\t\t\t}\n\t\t}\n\t}\n\n\t/** Takes the next stderr bytes into the tail and passes them to `onStderr`. */\n\treadStderr(chunk: Uint8Array): void {\n\t\tconst maxStderrBytes = this.options.maxStderrBytes ?? DEFAULT_MAX_STDERR_BYTES;\n\t\tthis.stderrBuffer = Buffer.concat([this.stderrBuffer, chunk]);\n\t\tif (this.stderrBuffer.length > maxStderrBytes) this.stderrBuffer = this.stderrBuffer.subarray(-maxStderrBytes);\n\t\tthis.options.onStderr?.(Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength).toString(\"utf8\"));\n\t}\n\n\t/** Ends stdout: a message without its newline is reported as an error and dropped. */\n\tend(): void {\n\t\tif (Buffer.concat(this.stdoutChunks).toString(\"utf8\").trim()) {\n\t\t\tthis.options.onError(new Error(\"MCP stdio server closed with an incomplete JSON-RPC message\"));\n\t\t}\n\t\tthis.stdoutChunks = [];\n\t\tthis.stdoutPendingBytes = 0;\n\t}\n}\n"]}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type JsonRpcMessage } from "../protocol/jsonrpc.ts";
|
|
2
|
+
import { type McpTransport, TransportEvents } from "./transport.ts";
|
|
3
|
+
/** A running program's stdio, as `@diffra-ai/env`'s `RemoteProcess` provides it. */
|
|
4
|
+
export interface McpPipe {
|
|
5
|
+
write(data: Uint8Array | string): void;
|
|
6
|
+
closeStdin(): void;
|
|
7
|
+
/** Kill the program (and what it started). The pipe owns cleaning up what the program started. */
|
|
8
|
+
close(): void;
|
|
9
|
+
/** Settles when the program has ended, whatever the reason; it must also settle after `close()`. */
|
|
10
|
+
readonly exited: Promise<unknown>;
|
|
11
|
+
}
|
|
12
|
+
/** Handlers `PipeTransportOptions.open` connects to the program's output. */
|
|
13
|
+
export interface McpPipeHandlers {
|
|
14
|
+
onStdout(bytes: Uint8Array): void;
|
|
15
|
+
onStderr(bytes: Uint8Array): void;
|
|
16
|
+
}
|
|
17
|
+
export interface PipeTransportOptions {
|
|
18
|
+
/** Start the program; `onStdout`/`onStderr` receive its output from then on. */
|
|
19
|
+
open(handlers: McpPipeHandlers): Promise<McpPipe>;
|
|
20
|
+
maxMessageBytes?: number;
|
|
21
|
+
maxStderrBytes?: number;
|
|
22
|
+
onStderr?: (chunk: string) => void;
|
|
23
|
+
/** After closing stdin, how long the program may take to exit before `close()` kills it. Default 2000. */
|
|
24
|
+
closeTimeoutMs?: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* MCP's stdio transport over a program that something else runs, for example on another machine through diffra-env.
|
|
28
|
+
* The transport closes when the program ends; a rejected `exited` that `close()` did not cause is reported as an
|
|
29
|
+
* error first.
|
|
30
|
+
*/
|
|
31
|
+
export declare class PipeTransport extends TransportEvents implements McpTransport {
|
|
32
|
+
readonly options: Readonly<PipeTransportOptions>;
|
|
33
|
+
private readonly lines;
|
|
34
|
+
private pipe;
|
|
35
|
+
private started;
|
|
36
|
+
private closed;
|
|
37
|
+
private ended;
|
|
38
|
+
constructor(options: PipeTransportOptions);
|
|
39
|
+
/** The last `maxStderrBytes` (default 64 KiB) of the program's stderr. */
|
|
40
|
+
get stderr(): string;
|
|
41
|
+
start(): Promise<void>;
|
|
42
|
+
send(message: JsonRpcMessage): Promise<void>;
|
|
43
|
+
close(): Promise<void>;
|
|
44
|
+
/** The program ended: report an unfinished message, then close. */
|
|
45
|
+
private exit;
|
|
46
|
+
/** `exited` rejected: an error unless `close()` caused it, then the same as an exit. */
|
|
47
|
+
private fail;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=pipe.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pipe.d.ts","sourceRoot":"","sources":["../../src/transports/pipe.ts"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