poe-code 3.0.178 → 3.0.179
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/index.js +6 -2
- package/dist/index.js.map +2 -2
- package/package.json +6 -2
- package/packages/cmdkit/dist/cli.compile-check.d.ts +1 -0
- package/packages/cmdkit/dist/cli.compile-check.js +26 -0
- package/packages/cmdkit/dist/cli.d.ts +12 -0
- package/packages/cmdkit/dist/cli.js +2306 -0
- package/packages/cmdkit/dist/cli.js.map +7 -0
- package/packages/cmdkit/dist/index.compile-check.d.ts +1 -0
- package/packages/cmdkit/dist/index.compile-check.js +50 -0
- package/packages/cmdkit/dist/index.d.ts +164 -0
- package/packages/cmdkit/dist/index.js +518 -0
- package/packages/cmdkit/dist/index.js.map +7 -0
- package/packages/cmdkit/dist/mcp.compile-check.d.ts +1 -0
- package/packages/cmdkit/dist/mcp.compile-check.js +26 -0
- package/packages/cmdkit/dist/mcp.d.ts +16 -0
- package/packages/cmdkit/dist/mcp.js +1153 -0
- package/packages/cmdkit/dist/mcp.js.map +7 -0
- package/packages/cmdkit/dist/renderer.d.ts +5 -0
- package/packages/cmdkit/dist/renderer.js +164 -0
- package/packages/cmdkit/dist/renderer.js.map +7 -0
- package/packages/cmdkit/dist/sdk.compile-check.d.ts +1 -0
- package/packages/cmdkit/dist/sdk.compile-check.js +79 -0
- package/packages/cmdkit/dist/sdk.d.ts +63 -0
- package/packages/cmdkit/dist/sdk.js +314 -0
- package/packages/cmdkit/dist/sdk.js.map +7 -0
- package/packages/cmdkit-schema/dist/index.compile-check.d.ts +1 -0
- package/packages/cmdkit-schema/dist/index.compile-check.js +11 -0
- package/packages/cmdkit-schema/dist/index.d.ts +81 -0
- package/packages/cmdkit-schema/dist/index.js +130 -0
- package/packages/design-system/dist/acp/components.d.ts +11 -0
- package/packages/design-system/dist/acp/components.js +121 -0
- package/packages/design-system/dist/acp/index.d.ts +3 -0
- package/packages/design-system/dist/acp/index.js +2 -0
- package/packages/design-system/dist/acp/writer.d.ts +13 -0
- package/packages/design-system/dist/acp/writer.js +21 -0
- package/packages/design-system/dist/components/command-errors.d.ts +16 -0
- package/packages/design-system/dist/components/command-errors.js +22 -0
- package/packages/design-system/dist/components/help-formatter.d.ts +20 -0
- package/packages/design-system/dist/components/help-formatter.js +27 -0
- package/packages/design-system/dist/components/index.d.ts +10 -0
- package/packages/design-system/dist/components/index.js +7 -0
- package/packages/design-system/dist/components/logger.d.ts +11 -0
- package/packages/design-system/dist/components/logger.js +60 -0
- package/packages/design-system/dist/components/symbols.d.ts +12 -0
- package/packages/design-system/dist/components/symbols.js +71 -0
- package/packages/design-system/dist/components/table.d.ts +13 -0
- package/packages/design-system/dist/components/table.js +74 -0
- package/packages/design-system/dist/components/text.d.ts +14 -0
- package/packages/design-system/dist/components/text.js +104 -0
- package/packages/design-system/dist/dashboard/ansi.d.ts +18 -0
- package/packages/design-system/dist/dashboard/ansi.js +298 -0
- package/packages/design-system/dist/dashboard/buffer.d.ts +25 -0
- package/packages/design-system/dist/dashboard/buffer.js +189 -0
- package/packages/design-system/dist/dashboard/components/border.d.ts +9 -0
- package/packages/design-system/dist/dashboard/components/border.js +123 -0
- package/packages/design-system/dist/dashboard/components/footer.d.ts +8 -0
- package/packages/design-system/dist/dashboard/components/footer.js +58 -0
- package/packages/design-system/dist/dashboard/components/output-pane.d.ts +21 -0
- package/packages/design-system/dist/dashboard/components/output-pane.js +323 -0
- package/packages/design-system/dist/dashboard/components/stats-pane.d.ts +7 -0
- package/packages/design-system/dist/dashboard/components/stats-pane.js +120 -0
- package/packages/design-system/dist/dashboard/dashboard.d.ts +20 -0
- package/packages/design-system/dist/dashboard/dashboard.js +187 -0
- package/packages/design-system/dist/dashboard/demo.d.ts +13 -0
- package/packages/design-system/dist/dashboard/demo.js +145 -0
- package/packages/design-system/dist/dashboard/index.d.ts +7 -0
- package/packages/design-system/dist/dashboard/index.js +3 -0
- package/packages/design-system/dist/dashboard/keymap.d.ts +3 -0
- package/packages/design-system/dist/dashboard/keymap.js +114 -0
- package/packages/design-system/dist/dashboard/layout.d.ts +25 -0
- package/packages/design-system/dist/dashboard/layout.js +79 -0
- package/packages/design-system/dist/dashboard/snapshot.d.ts +10 -0
- package/packages/design-system/dist/dashboard/snapshot.js +72 -0
- package/packages/design-system/dist/dashboard/store.d.ts +9 -0
- package/packages/design-system/dist/dashboard/store.js +107 -0
- package/packages/design-system/dist/dashboard/terminal.d.ts +35 -0
- package/packages/design-system/dist/dashboard/terminal.js +215 -0
- package/packages/design-system/dist/dashboard/types.d.ts +45 -0
- package/packages/design-system/dist/dashboard/types.js +1 -0
- package/packages/design-system/dist/index.d.ts +33 -0
- package/packages/design-system/dist/index.js +31 -0
- package/packages/design-system/dist/internal/output-format.d.ts +6 -0
- package/packages/design-system/dist/internal/output-format.js +22 -0
- package/packages/design-system/dist/internal/strip-ansi.d.ts +1 -0
- package/packages/design-system/dist/internal/strip-ansi.js +3 -0
- package/packages/design-system/dist/internal/theme-detect.d.ts +11 -0
- package/packages/design-system/dist/internal/theme-detect.js +49 -0
- package/packages/design-system/dist/prompts/index.d.ts +66 -0
- package/packages/design-system/dist/prompts/index.js +132 -0
- package/packages/design-system/dist/prompts/primitives/cancel.d.ts +2 -0
- package/packages/design-system/dist/prompts/primitives/cancel.js +9 -0
- package/packages/design-system/dist/prompts/primitives/intro.d.ts +1 -0
- package/packages/design-system/dist/prompts/primitives/intro.js +15 -0
- package/packages/design-system/dist/prompts/primitives/log.d.ts +18 -0
- package/packages/design-system/dist/prompts/primitives/log.js +101 -0
- package/packages/design-system/dist/prompts/primitives/note.d.ts +1 -0
- package/packages/design-system/dist/prompts/primitives/note.js +39 -0
- package/packages/design-system/dist/prompts/primitives/outro.d.ts +1 -0
- package/packages/design-system/dist/prompts/primitives/outro.js +16 -0
- package/packages/design-system/dist/prompts/primitives/spinner.d.ts +6 -0
- package/packages/design-system/dist/prompts/primitives/spinner.js +74 -0
- package/packages/design-system/dist/prompts/theme.d.ts +11 -0
- package/packages/design-system/dist/prompts/theme.js +12 -0
- package/packages/design-system/dist/static/index.d.ts +4 -0
- package/packages/design-system/dist/static/index.js +2 -0
- package/packages/design-system/dist/static/menu.d.ts +11 -0
- package/packages/design-system/dist/static/menu.js +36 -0
- package/packages/design-system/dist/static/spinner.d.ts +14 -0
- package/packages/design-system/dist/static/spinner.js +46 -0
- package/packages/design-system/dist/terminal-markdown/ast.d.ts +84 -0
- package/packages/design-system/dist/terminal-markdown/ast.js +1 -0
- package/packages/design-system/dist/terminal-markdown/demo-content.d.ts +2 -0
- package/packages/design-system/dist/terminal-markdown/demo-content.js +139 -0
- package/packages/design-system/dist/terminal-markdown/index.d.ts +6 -0
- package/packages/design-system/dist/terminal-markdown/index.js +8 -0
- package/packages/design-system/dist/terminal-markdown/parser/block.d.ts +6 -0
- package/packages/design-system/dist/terminal-markdown/parser/block.js +1205 -0
- package/packages/design-system/dist/terminal-markdown/parser/frontmatter.d.ts +6 -0
- package/packages/design-system/dist/terminal-markdown/parser/frontmatter.js +395 -0
- package/packages/design-system/dist/terminal-markdown/parser/inline.d.ts +6 -0
- package/packages/design-system/dist/terminal-markdown/parser/inline.js +1087 -0
- package/packages/design-system/dist/terminal-markdown/parser.d.ts +5 -0
- package/packages/design-system/dist/terminal-markdown/parser.js +13 -0
- package/packages/design-system/dist/terminal-markdown/renderer.d.ts +6 -0
- package/packages/design-system/dist/terminal-markdown/renderer.js +572 -0
- package/packages/design-system/dist/terminal-markdown/testing/theme-render-fixture.d.ts +1 -0
- package/packages/design-system/dist/terminal-markdown/testing/theme-render-fixture.js +27 -0
- package/packages/design-system/dist/tokens/colors.d.ts +35 -0
- package/packages/design-system/dist/tokens/colors.js +34 -0
- package/packages/design-system/dist/tokens/index.d.ts +4 -0
- package/packages/design-system/dist/tokens/index.js +4 -0
- package/packages/design-system/dist/tokens/spacing.d.ts +6 -0
- package/packages/design-system/dist/tokens/spacing.js +6 -0
- package/packages/design-system/dist/tokens/typography.d.ts +7 -0
- package/packages/design-system/dist/tokens/typography.js +8 -0
- package/packages/design-system/dist/tokens/widths.d.ts +5 -0
- package/packages/design-system/dist/tokens/widths.js +5 -0
- package/packages/tiny-stdio-mcp-server/dist/content/audio.d.ts +14 -0
- package/packages/tiny-stdio-mcp-server/dist/content/audio.js +76 -0
- package/packages/tiny-stdio-mcp-server/dist/content/convert.d.ts +16 -0
- package/packages/tiny-stdio-mcp-server/dist/content/convert.js +42 -0
- package/packages/tiny-stdio-mcp-server/dist/content/file-type.d.ts +11 -0
- package/packages/tiny-stdio-mcp-server/dist/content/file-type.js +93 -0
- package/packages/tiny-stdio-mcp-server/dist/content/file.d.ts +26 -0
- package/packages/tiny-stdio-mcp-server/dist/content/file.js +94 -0
- package/packages/tiny-stdio-mcp-server/dist/content/image.d.ts +14 -0
- package/packages/tiny-stdio-mcp-server/dist/content/image.js +64 -0
- package/packages/tiny-stdio-mcp-server/dist/content/index.d.ts +5 -0
- package/packages/tiny-stdio-mcp-server/dist/content/index.js +8 -0
- package/packages/tiny-stdio-mcp-server/dist/content/mime.d.ts +1 -0
- package/packages/tiny-stdio-mcp-server/dist/content/mime.js +1 -0
- package/packages/tiny-stdio-mcp-server/dist/index.d.ts +9 -0
- package/packages/tiny-stdio-mcp-server/dist/index.js +7 -0
- package/packages/tiny-stdio-mcp-server/dist/jsonrpc.d.ts +14 -0
- package/packages/tiny-stdio-mcp-server/dist/jsonrpc.js +99 -0
- package/packages/tiny-stdio-mcp-server/dist/schema.d.ts +19 -0
- package/packages/tiny-stdio-mcp-server/dist/schema.js +18 -0
- package/packages/tiny-stdio-mcp-server/dist/server.d.ts +13 -0
- package/packages/tiny-stdio-mcp-server/dist/server.js +226 -0
- package/packages/tiny-stdio-mcp-server/dist/testing.d.ts +7 -0
- package/packages/tiny-stdio-mcp-server/dist/testing.js +20 -0
- package/packages/tiny-stdio-mcp-server/dist/types.d.ts +119 -0
- package/packages/tiny-stdio-mcp-server/dist/types.js +16 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { JSON_RPC_ERROR_CODES } from "./types.js";
|
|
2
|
+
export function parseMessage(line) {
|
|
3
|
+
let parsed;
|
|
4
|
+
try {
|
|
5
|
+
parsed = JSON.parse(line);
|
|
6
|
+
}
|
|
7
|
+
catch {
|
|
8
|
+
return {
|
|
9
|
+
success: false,
|
|
10
|
+
error: {
|
|
11
|
+
code: JSON_RPC_ERROR_CODES.PARSE_ERROR,
|
|
12
|
+
message: "Parse error",
|
|
13
|
+
},
|
|
14
|
+
id: null,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
if (typeof parsed !== "object" ||
|
|
18
|
+
parsed === null ||
|
|
19
|
+
Array.isArray(parsed)) {
|
|
20
|
+
return {
|
|
21
|
+
success: false,
|
|
22
|
+
error: {
|
|
23
|
+
code: JSON_RPC_ERROR_CODES.INVALID_REQUEST,
|
|
24
|
+
message: "Invalid Request",
|
|
25
|
+
},
|
|
26
|
+
id: null,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
const obj = parsed;
|
|
30
|
+
const hasId = "id" in obj;
|
|
31
|
+
const id = typeof obj.id === "string" || typeof obj.id === "number" ? obj.id : null;
|
|
32
|
+
if (obj.jsonrpc !== "2.0") {
|
|
33
|
+
return {
|
|
34
|
+
success: false,
|
|
35
|
+
error: {
|
|
36
|
+
code: JSON_RPC_ERROR_CODES.INVALID_REQUEST,
|
|
37
|
+
message: "Invalid Request",
|
|
38
|
+
},
|
|
39
|
+
id,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
if (typeof obj.method !== "string") {
|
|
43
|
+
return {
|
|
44
|
+
success: false,
|
|
45
|
+
error: {
|
|
46
|
+
code: JSON_RPC_ERROR_CODES.INVALID_REQUEST,
|
|
47
|
+
message: "Invalid Request",
|
|
48
|
+
},
|
|
49
|
+
id,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
if (!hasId) {
|
|
53
|
+
return {
|
|
54
|
+
success: true,
|
|
55
|
+
isNotification: true,
|
|
56
|
+
request: {
|
|
57
|
+
jsonrpc: "2.0",
|
|
58
|
+
method: obj.method,
|
|
59
|
+
params: obj.params,
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
if (id === null) {
|
|
64
|
+
return {
|
|
65
|
+
success: false,
|
|
66
|
+
error: {
|
|
67
|
+
code: JSON_RPC_ERROR_CODES.INVALID_REQUEST,
|
|
68
|
+
message: "Invalid Request",
|
|
69
|
+
},
|
|
70
|
+
id: null,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
success: true,
|
|
75
|
+
isNotification: false,
|
|
76
|
+
request: {
|
|
77
|
+
jsonrpc: "2.0",
|
|
78
|
+
id,
|
|
79
|
+
method: obj.method,
|
|
80
|
+
params: obj.params,
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
export function formatSuccessResponse(id, result) {
|
|
85
|
+
const response = {
|
|
86
|
+
jsonrpc: "2.0",
|
|
87
|
+
id,
|
|
88
|
+
result,
|
|
89
|
+
};
|
|
90
|
+
return JSON.stringify(response);
|
|
91
|
+
}
|
|
92
|
+
export function formatErrorResponse(id, error) {
|
|
93
|
+
const response = {
|
|
94
|
+
jsonrpc: "2.0",
|
|
95
|
+
id,
|
|
96
|
+
error,
|
|
97
|
+
};
|
|
98
|
+
return JSON.stringify(response);
|
|
99
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { JSONSchema } from "./types.js";
|
|
2
|
+
type SchemaPropertyType = "string" | "number" | "boolean" | "object" | "array";
|
|
3
|
+
interface SchemaPropertyDef {
|
|
4
|
+
type: SchemaPropertyType;
|
|
5
|
+
description?: string;
|
|
6
|
+
optional?: boolean;
|
|
7
|
+
}
|
|
8
|
+
type SchemaDefinition = Record<string, SchemaPropertyDef>;
|
|
9
|
+
type InferType<T extends SchemaPropertyType> = T extends "string" ? string : T extends "number" ? number : T extends "boolean" ? boolean : T extends "object" ? Record<string, unknown> : T extends "array" ? unknown[] : never;
|
|
10
|
+
type InferSchema<T extends SchemaDefinition> = {
|
|
11
|
+
[K in keyof T as T[K]["optional"] extends true ? never : K]: InferType<T[K]["type"]>;
|
|
12
|
+
} & {
|
|
13
|
+
[K in keyof T as T[K]["optional"] extends true ? K : never]?: InferType<T[K]["type"]>;
|
|
14
|
+
};
|
|
15
|
+
export interface TypedSchema<T> extends JSONSchema {
|
|
16
|
+
__type?: T;
|
|
17
|
+
}
|
|
18
|
+
export declare function defineSchema<T extends SchemaDefinition>(definition: T): TypedSchema<InferSchema<T>>;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function defineSchema(definition) {
|
|
2
|
+
const properties = {};
|
|
3
|
+
const required = [];
|
|
4
|
+
for (const [key, prop] of Object.entries(definition)) {
|
|
5
|
+
properties[key] = {
|
|
6
|
+
type: prop.type,
|
|
7
|
+
...(prop.description !== undefined && { description: prop.description }),
|
|
8
|
+
};
|
|
9
|
+
if (!prop.optional) {
|
|
10
|
+
required.push(key);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return {
|
|
14
|
+
type: "object",
|
|
15
|
+
properties,
|
|
16
|
+
required,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ServerOptions, ToolHandler, HandleResult, Transport, SDKTransport, JSONRPCNotification } from "./types.js";
|
|
2
|
+
import type { TypedSchema } from "./schema.js";
|
|
3
|
+
export interface Server {
|
|
4
|
+
tool<T>(name: string, description: string, inputSchema: TypedSchema<T>, handler: ToolHandler<T>): Server;
|
|
5
|
+
onNotification(listener: (notification: JSONRPCNotification) => void): () => void;
|
|
6
|
+
removeTool(name: string): boolean;
|
|
7
|
+
notifyToolsChanged(): Promise<void>;
|
|
8
|
+
handleMessage(method: string, params?: Record<string, unknown>): Promise<HandleResult>;
|
|
9
|
+
listen(): Promise<void>;
|
|
10
|
+
connect(transport: Transport): Promise<void>;
|
|
11
|
+
connectSDK(transport: SDKTransport): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
export declare function createServer(options: ServerOptions): Server;
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import * as readline from "readline";
|
|
2
|
+
import { JSON_RPC_ERROR_CODES, ToolError } from "./types.js";
|
|
3
|
+
import { parseMessage, formatSuccessResponse, formatErrorResponse, } from "./jsonrpc.js";
|
|
4
|
+
import { toContentBlocks } from "./content/convert.js";
|
|
5
|
+
const PROTOCOL_VERSION = "2025-11-25";
|
|
6
|
+
export function createServer(options) {
|
|
7
|
+
const tools = new Map();
|
|
8
|
+
const notificationListeners = new Set();
|
|
9
|
+
let initialized = false;
|
|
10
|
+
const handleMessage = async (method, params) => {
|
|
11
|
+
// Allow ping and initialize before initialization
|
|
12
|
+
if (method === "ping") {
|
|
13
|
+
return { result: {} };
|
|
14
|
+
}
|
|
15
|
+
if (method === "initialize") {
|
|
16
|
+
initialized = true;
|
|
17
|
+
const requestedProtocol = typeof params?.protocolVersion === "string"
|
|
18
|
+
? params.protocolVersion
|
|
19
|
+
: null;
|
|
20
|
+
const result = {
|
|
21
|
+
protocolVersion: requestedProtocol ?? PROTOCOL_VERSION,
|
|
22
|
+
capabilities: {
|
|
23
|
+
tools: {
|
|
24
|
+
listChanged: true,
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
serverInfo: {
|
|
28
|
+
name: options.name,
|
|
29
|
+
version: options.version,
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
return { result };
|
|
33
|
+
}
|
|
34
|
+
if (method === "notifications/initialized") {
|
|
35
|
+
return { result: undefined };
|
|
36
|
+
}
|
|
37
|
+
// All other methods require initialization
|
|
38
|
+
if (!initialized) {
|
|
39
|
+
return {
|
|
40
|
+
error: {
|
|
41
|
+
code: JSON_RPC_ERROR_CODES.INVALID_REQUEST,
|
|
42
|
+
message: "Server not initialized",
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
if (method === "tools/list") {
|
|
47
|
+
const toolList = [];
|
|
48
|
+
for (const tool of tools.values()) {
|
|
49
|
+
toolList.push({
|
|
50
|
+
name: tool.name,
|
|
51
|
+
description: tool.description,
|
|
52
|
+
inputSchema: tool.inputSchema,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return { result: { tools: toolList } };
|
|
56
|
+
}
|
|
57
|
+
if (method === "tools/call") {
|
|
58
|
+
const toolName = params?.name;
|
|
59
|
+
const toolArgs = params?.arguments || {};
|
|
60
|
+
if (!toolName) {
|
|
61
|
+
return {
|
|
62
|
+
error: {
|
|
63
|
+
code: JSON_RPC_ERROR_CODES.INVALID_PARAMS,
|
|
64
|
+
message: "Tool name required",
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
const tool = tools.get(toolName);
|
|
69
|
+
if (!tool) {
|
|
70
|
+
return {
|
|
71
|
+
error: {
|
|
72
|
+
code: JSON_RPC_ERROR_CODES.INVALID_PARAMS,
|
|
73
|
+
message: `Tool not found: ${toolName}`,
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
const handlerResult = await tool.handler(toolArgs);
|
|
79
|
+
const result = { content: toContentBlocks(handlerResult) };
|
|
80
|
+
return { result };
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
if (err instanceof ToolError) {
|
|
84
|
+
return {
|
|
85
|
+
error: {
|
|
86
|
+
code: err.code,
|
|
87
|
+
message: err.message,
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
92
|
+
const result = {
|
|
93
|
+
content: [{ type: "text", text: `Error: ${errorMessage}` }],
|
|
94
|
+
isError: true,
|
|
95
|
+
};
|
|
96
|
+
return { result };
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
error: {
|
|
101
|
+
code: JSON_RPC_ERROR_CODES.METHOD_NOT_FOUND,
|
|
102
|
+
message: "Method not found",
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
const processLine = async (line, write) => {
|
|
107
|
+
const parsed = parseMessage(line);
|
|
108
|
+
if (!parsed.success) {
|
|
109
|
+
write(formatErrorResponse(parsed.id, parsed.error) + "\n");
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const { request, isNotification } = parsed;
|
|
113
|
+
const { result, error } = await server.handleMessage(request.method, request.params);
|
|
114
|
+
if (isNotification) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const requestWithId = request;
|
|
118
|
+
if (error) {
|
|
119
|
+
write(formatErrorResponse(requestWithId.id, error) + "\n");
|
|
120
|
+
}
|
|
121
|
+
else if (result !== undefined) {
|
|
122
|
+
write(formatSuccessResponse(requestWithId.id, result) + "\n");
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const broadcastNotification = (method) => {
|
|
126
|
+
const notification = {
|
|
127
|
+
jsonrpc: "2.0",
|
|
128
|
+
method,
|
|
129
|
+
};
|
|
130
|
+
for (const listener of notificationListeners) {
|
|
131
|
+
listener(notification);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
const server = {
|
|
135
|
+
tool(name, description, inputSchema, handler) {
|
|
136
|
+
tools.set(name, {
|
|
137
|
+
name,
|
|
138
|
+
description,
|
|
139
|
+
inputSchema: inputSchema,
|
|
140
|
+
handler: handler,
|
|
141
|
+
});
|
|
142
|
+
return server;
|
|
143
|
+
},
|
|
144
|
+
onNotification(listener) {
|
|
145
|
+
notificationListeners.add(listener);
|
|
146
|
+
return () => {
|
|
147
|
+
notificationListeners.delete(listener);
|
|
148
|
+
};
|
|
149
|
+
},
|
|
150
|
+
removeTool(name) {
|
|
151
|
+
return tools.delete(name);
|
|
152
|
+
},
|
|
153
|
+
async notifyToolsChanged() {
|
|
154
|
+
if (initialized) {
|
|
155
|
+
broadcastNotification("notifications/tools/list_changed");
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
handleMessage,
|
|
159
|
+
async listen() {
|
|
160
|
+
return server.connect({
|
|
161
|
+
readable: process.stdin,
|
|
162
|
+
writable: process.stdout,
|
|
163
|
+
});
|
|
164
|
+
},
|
|
165
|
+
async connect(transport) {
|
|
166
|
+
return new Promise((resolve) => {
|
|
167
|
+
const unsubscribe = server.onNotification((notification) => {
|
|
168
|
+
transport.writable.write(`${JSON.stringify(notification)}\n`);
|
|
169
|
+
});
|
|
170
|
+
const rl = readline.createInterface({
|
|
171
|
+
input: transport.readable,
|
|
172
|
+
crlfDelay: Infinity,
|
|
173
|
+
});
|
|
174
|
+
rl.on("line", (line) => {
|
|
175
|
+
processLine(line, (data) => transport.writable.write(data));
|
|
176
|
+
});
|
|
177
|
+
rl.on("close", () => {
|
|
178
|
+
unsubscribe();
|
|
179
|
+
resolve();
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
},
|
|
183
|
+
async connectSDK(transport) {
|
|
184
|
+
return new Promise((resolve) => {
|
|
185
|
+
const unsubscribe = server.onNotification((notification) => {
|
|
186
|
+
void transport.send(notification);
|
|
187
|
+
});
|
|
188
|
+
transport.onmessage = async (message) => {
|
|
189
|
+
// Ignore responses (we only handle requests/notifications)
|
|
190
|
+
if (!("method" in message)) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
// Handle notifications (no id) - don't respond
|
|
194
|
+
if (!("id" in message) || message.id === undefined) {
|
|
195
|
+
await server.handleMessage(message.method, message.params);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const request = message;
|
|
199
|
+
const { result, error } = await server.handleMessage(request.method, request.params);
|
|
200
|
+
if (error) {
|
|
201
|
+
const response = {
|
|
202
|
+
jsonrpc: "2.0",
|
|
203
|
+
id: request.id,
|
|
204
|
+
error,
|
|
205
|
+
};
|
|
206
|
+
await transport.send(response);
|
|
207
|
+
}
|
|
208
|
+
else if (result !== undefined) {
|
|
209
|
+
const response = {
|
|
210
|
+
jsonrpc: "2.0",
|
|
211
|
+
id: request.id,
|
|
212
|
+
result,
|
|
213
|
+
};
|
|
214
|
+
await transport.send(response);
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
transport.onclose = () => {
|
|
218
|
+
unsubscribe();
|
|
219
|
+
resolve();
|
|
220
|
+
};
|
|
221
|
+
transport.start();
|
|
222
|
+
});
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
return server;
|
|
226
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
|
+
import type { Server } from "./server.js";
|
|
3
|
+
export interface TestPair {
|
|
4
|
+
client: Client;
|
|
5
|
+
cleanup: () => Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
export declare function createTestPair(server: Server): Promise<TestPair>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
|
+
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
|
|
3
|
+
export async function createTestPair(server) {
|
|
4
|
+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
5
|
+
const client = new Client({
|
|
6
|
+
name: "test-client",
|
|
7
|
+
version: "1.0.0",
|
|
8
|
+
});
|
|
9
|
+
// Start server connection (runs in background)
|
|
10
|
+
const serverPromise = server.connectSDK(serverTransport);
|
|
11
|
+
// Connect client
|
|
12
|
+
await client.connect(clientTransport);
|
|
13
|
+
const cleanup = async () => {
|
|
14
|
+
await client.close();
|
|
15
|
+
await clientTransport.close();
|
|
16
|
+
await serverTransport.close();
|
|
17
|
+
await serverPromise;
|
|
18
|
+
};
|
|
19
|
+
return { client, cleanup };
|
|
20
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
export interface JSONRPCRequest {
|
|
2
|
+
jsonrpc: "2.0";
|
|
3
|
+
id: string | number;
|
|
4
|
+
method: string;
|
|
5
|
+
params?: Record<string, unknown>;
|
|
6
|
+
}
|
|
7
|
+
export interface JSONRPCResponse {
|
|
8
|
+
jsonrpc: "2.0";
|
|
9
|
+
id: string | number | null;
|
|
10
|
+
result?: unknown;
|
|
11
|
+
error?: JSONRPCError;
|
|
12
|
+
}
|
|
13
|
+
export interface JSONRPCError {
|
|
14
|
+
code: number;
|
|
15
|
+
message: string;
|
|
16
|
+
data?: unknown;
|
|
17
|
+
}
|
|
18
|
+
export declare const JSON_RPC_ERROR_CODES: {
|
|
19
|
+
readonly PARSE_ERROR: -32700;
|
|
20
|
+
readonly INVALID_REQUEST: -32600;
|
|
21
|
+
readonly METHOD_NOT_FOUND: -32601;
|
|
22
|
+
readonly INVALID_PARAMS: -32602;
|
|
23
|
+
readonly INTERNAL_ERROR: -32603;
|
|
24
|
+
};
|
|
25
|
+
export declare class ToolError extends Error {
|
|
26
|
+
readonly code: number;
|
|
27
|
+
constructor(code: number, message: string);
|
|
28
|
+
}
|
|
29
|
+
export interface ToolsCapability {
|
|
30
|
+
listChanged?: boolean;
|
|
31
|
+
}
|
|
32
|
+
export interface InitializeResult {
|
|
33
|
+
protocolVersion: string;
|
|
34
|
+
capabilities: {
|
|
35
|
+
tools?: ToolsCapability;
|
|
36
|
+
};
|
|
37
|
+
serverInfo: {
|
|
38
|
+
name: string;
|
|
39
|
+
version: string;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export interface Tool {
|
|
43
|
+
name: string;
|
|
44
|
+
description: string;
|
|
45
|
+
inputSchema: JSONSchema;
|
|
46
|
+
}
|
|
47
|
+
export interface CallToolResult {
|
|
48
|
+
content: ContentItem[];
|
|
49
|
+
isError?: boolean;
|
|
50
|
+
}
|
|
51
|
+
export interface HandleResult {
|
|
52
|
+
result?: unknown;
|
|
53
|
+
error?: {
|
|
54
|
+
code: number;
|
|
55
|
+
message: string;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export type ContentItem = {
|
|
59
|
+
type: "text";
|
|
60
|
+
text: string;
|
|
61
|
+
} | {
|
|
62
|
+
type: "image";
|
|
63
|
+
data: string;
|
|
64
|
+
mimeType: string;
|
|
65
|
+
} | {
|
|
66
|
+
type: "audio";
|
|
67
|
+
data: string;
|
|
68
|
+
mimeType: string;
|
|
69
|
+
} | {
|
|
70
|
+
type: "resource";
|
|
71
|
+
resource: {
|
|
72
|
+
uri: string;
|
|
73
|
+
mimeType: string;
|
|
74
|
+
text: string;
|
|
75
|
+
} | {
|
|
76
|
+
uri: string;
|
|
77
|
+
mimeType: string;
|
|
78
|
+
blob: string;
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
export interface JSONSchema {
|
|
82
|
+
type: "object";
|
|
83
|
+
properties: Record<string, JSONSchemaProperty>;
|
|
84
|
+
required?: string[];
|
|
85
|
+
}
|
|
86
|
+
export interface JSONSchemaProperty {
|
|
87
|
+
type: "string" | "number" | "boolean" | "object" | "array";
|
|
88
|
+
description?: string;
|
|
89
|
+
}
|
|
90
|
+
export interface ServerOptions {
|
|
91
|
+
name: string;
|
|
92
|
+
version: string;
|
|
93
|
+
}
|
|
94
|
+
import type { ToolReturn } from "./content/index.js";
|
|
95
|
+
export type ToolHandler<T = Record<string, unknown>> = (args: T) => Promise<ToolReturn> | ToolReturn;
|
|
96
|
+
export interface ToolDefinition<T = Record<string, unknown>> {
|
|
97
|
+
name: string;
|
|
98
|
+
description: string;
|
|
99
|
+
inputSchema: JSONSchema;
|
|
100
|
+
handler: ToolHandler<T>;
|
|
101
|
+
}
|
|
102
|
+
export interface Transport {
|
|
103
|
+
readable: NodeJS.ReadableStream;
|
|
104
|
+
writable: NodeJS.WritableStream;
|
|
105
|
+
}
|
|
106
|
+
export interface SDKTransport {
|
|
107
|
+
onmessage?: (message: JSONRPCMessage) => void;
|
|
108
|
+
onclose?: () => void;
|
|
109
|
+
onerror?: (error: Error) => void;
|
|
110
|
+
start: () => Promise<void>;
|
|
111
|
+
close: () => Promise<void>;
|
|
112
|
+
send: (message: JSONRPCMessage) => Promise<void>;
|
|
113
|
+
}
|
|
114
|
+
export type JSONRPCMessage = JSONRPCRequest | JSONRPCResponse | JSONRPCNotification;
|
|
115
|
+
export interface JSONRPCNotification {
|
|
116
|
+
jsonrpc: "2.0";
|
|
117
|
+
method: string;
|
|
118
|
+
params?: Record<string, unknown>;
|
|
119
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// JSON-RPC error codes
|
|
2
|
+
export const JSON_RPC_ERROR_CODES = {
|
|
3
|
+
PARSE_ERROR: -32700,
|
|
4
|
+
INVALID_REQUEST: -32600,
|
|
5
|
+
METHOD_NOT_FOUND: -32601,
|
|
6
|
+
INVALID_PARAMS: -32602,
|
|
7
|
+
INTERNAL_ERROR: -32603
|
|
8
|
+
};
|
|
9
|
+
export class ToolError extends Error {
|
|
10
|
+
code;
|
|
11
|
+
constructor(code, message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.code = code;
|
|
14
|
+
this.name = "ToolError";
|
|
15
|
+
}
|
|
16
|
+
}
|