opc-agent 1.4.0 → 2.0.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 +25 -0
- package/README.md +91 -32
- package/dist/channels/email.d.ts +32 -26
- package/dist/channels/email.js +239 -62
- package/dist/channels/feishu.d.ts +21 -6
- package/dist/channels/feishu.js +225 -126
- package/dist/channels/telegram.d.ts +30 -9
- package/dist/channels/telegram.js +125 -33
- package/dist/channels/websocket.d.ts +46 -3
- package/dist/channels/websocket.js +306 -37
- package/dist/channels/wechat.d.ts +33 -13
- package/dist/channels/wechat.js +229 -42
- package/dist/cli.js +1127 -19
- package/dist/core/a2a.d.ts +17 -0
- package/dist/core/a2a.js +43 -1
- package/dist/core/agent.d.ts +39 -0
- package/dist/core/agent.js +228 -3
- package/dist/core/runtime.d.ts +7 -0
- package/dist/core/runtime.js +205 -2
- package/dist/core/sandbox.d.ts +26 -0
- package/dist/core/sandbox.js +117 -0
- package/dist/core/scheduler.d.ts +52 -0
- package/dist/core/scheduler.js +168 -0
- package/dist/core/subagent.d.ts +28 -0
- package/dist/core/subagent.js +65 -0
- package/dist/core/workflow-graph.d.ts +93 -0
- package/dist/core/workflow-graph.js +247 -0
- package/dist/daemon.d.ts +3 -0
- package/dist/daemon.js +134 -0
- package/dist/doctor.d.ts +15 -0
- package/dist/doctor.js +183 -0
- package/dist/eval/index.d.ts +65 -0
- package/dist/eval/index.js +191 -0
- package/dist/index.d.ts +37 -6
- package/dist/index.js +75 -3
- package/dist/plugins/content-filter.d.ts +7 -0
- package/dist/plugins/content-filter.js +25 -0
- package/dist/plugins/index.d.ts +42 -0
- package/dist/plugins/index.js +108 -2
- package/dist/plugins/logger.d.ts +6 -0
- package/dist/plugins/logger.js +20 -0
- package/dist/plugins/rate-limiter.d.ts +7 -0
- package/dist/plugins/rate-limiter.js +35 -0
- package/dist/protocols/a2a/client.d.ts +25 -0
- package/dist/protocols/a2a/client.js +115 -0
- package/dist/protocols/a2a/index.d.ts +6 -0
- package/dist/protocols/a2a/index.js +12 -0
- package/dist/protocols/a2a/server.d.ts +41 -0
- package/dist/protocols/a2a/server.js +295 -0
- package/dist/protocols/a2a/types.d.ts +91 -0
- package/dist/protocols/a2a/types.js +15 -0
- package/dist/protocols/a2a/utils.d.ts +6 -0
- package/dist/protocols/a2a/utils.js +47 -0
- package/dist/protocols/agui/client.d.ts +10 -0
- package/dist/protocols/agui/client.js +75 -0
- package/dist/protocols/agui/index.d.ts +4 -0
- package/dist/protocols/agui/index.js +25 -0
- package/dist/protocols/agui/server.d.ts +37 -0
- package/dist/protocols/agui/server.js +191 -0
- package/dist/protocols/agui/types.d.ts +107 -0
- package/dist/protocols/agui/types.js +17 -0
- package/dist/protocols/index.d.ts +2 -0
- package/dist/protocols/index.js +19 -0
- package/dist/protocols/mcp/agent-tools.d.ts +11 -0
- package/dist/protocols/mcp/agent-tools.js +129 -0
- package/dist/protocols/mcp/index.d.ts +5 -0
- package/dist/protocols/mcp/index.js +11 -0
- package/dist/protocols/mcp/server.d.ts +31 -0
- package/dist/protocols/mcp/server.js +248 -0
- package/dist/protocols/mcp/types.d.ts +92 -0
- package/dist/protocols/mcp/types.js +17 -0
- package/dist/providers/index.d.ts +5 -1
- package/dist/providers/index.js +16 -9
- package/dist/publish/index.d.ts +45 -0
- package/dist/publish/index.js +350 -0
- package/dist/schema/oad.d.ts +859 -67
- package/dist/schema/oad.js +47 -3
- package/dist/security/approval.d.ts +36 -0
- package/dist/security/approval.js +113 -0
- package/dist/security/index.d.ts +4 -0
- package/dist/security/index.js +8 -0
- package/dist/security/keys.d.ts +16 -0
- package/dist/security/keys.js +117 -0
- package/dist/skills/auto-learn.d.ts +28 -0
- package/dist/skills/auto-learn.js +257 -0
- package/dist/studio/server.d.ts +63 -0
- package/dist/studio/server.js +625 -0
- package/dist/studio-ui/index.html +662 -0
- package/dist/telemetry/index.d.ts +93 -0
- package/dist/telemetry/index.js +285 -0
- package/dist/tools/builtin/datetime.d.ts +3 -0
- package/dist/tools/builtin/datetime.js +44 -0
- package/dist/tools/builtin/file.d.ts +3 -0
- package/dist/tools/builtin/file.js +151 -0
- package/dist/tools/builtin/index.d.ts +15 -0
- package/dist/tools/builtin/index.js +30 -0
- package/dist/tools/builtin/shell.d.ts +3 -0
- package/dist/tools/builtin/shell.js +43 -0
- package/dist/tools/builtin/web.d.ts +3 -0
- package/dist/tools/builtin/web.js +37 -0
- package/dist/tools/mcp-client.d.ts +24 -0
- package/dist/tools/mcp-client.js +119 -0
- package/package.json +5 -3
- package/scripts/install.ps1 +31 -0
- package/scripts/install.sh +40 -0
- package/src/channels/email.ts +351 -177
- package/src/channels/feishu.ts +349 -236
- package/src/channels/telegram.ts +212 -90
- package/src/channels/websocket.ts +399 -87
- package/src/channels/wechat.ts +329 -149
- package/src/cli.ts +1201 -20
- package/src/core/a2a.ts +60 -0
- package/src/core/agent.ts +420 -152
- package/src/core/runtime.ts +174 -0
- package/src/core/sandbox.ts +143 -0
- package/src/core/scheduler.ts +187 -0
- package/src/core/subagent.ts +98 -0
- package/src/core/workflow-graph.ts +365 -0
- package/src/daemon.ts +96 -0
- package/src/doctor.ts +156 -0
- package/src/eval/index.ts +211 -0
- package/src/eval/suites/basic.json +16 -0
- package/src/eval/suites/memory.json +12 -0
- package/src/eval/suites/safety.json +14 -0
- package/src/index.ts +65 -6
- package/src/plugins/content-filter.ts +23 -0
- package/src/plugins/index.ts +133 -2
- package/src/plugins/logger.ts +18 -0
- package/src/plugins/rate-limiter.ts +38 -0
- package/src/protocols/a2a/client.ts +132 -0
- package/src/protocols/a2a/index.ts +8 -0
- package/src/protocols/a2a/server.ts +333 -0
- package/src/protocols/a2a/types.ts +88 -0
- package/src/protocols/a2a/utils.ts +50 -0
- package/src/protocols/agui/client.ts +83 -0
- package/src/protocols/agui/index.ts +4 -0
- package/src/protocols/agui/server.ts +218 -0
- package/src/protocols/agui/types.ts +153 -0
- package/src/protocols/index.ts +2 -0
- package/src/protocols/mcp/agent-tools.ts +134 -0
- package/src/protocols/mcp/index.ts +8 -0
- package/src/protocols/mcp/server.ts +262 -0
- package/src/protocols/mcp/types.ts +69 -0
- package/src/providers/index.ts +354 -339
- package/src/publish/index.ts +376 -0
- package/src/schema/oad.ts +204 -154
- package/src/security/approval.ts +131 -0
- package/src/security/index.ts +3 -0
- package/src/security/keys.ts +87 -0
- package/src/skills/auto-learn.ts +262 -0
- package/src/studio/server.ts +629 -0
- package/src/studio-ui/index.html +662 -0
- package/src/telemetry/index.ts +324 -0
- package/src/tools/builtin/datetime.ts +41 -0
- package/src/tools/builtin/file.ts +107 -0
- package/src/tools/builtin/index.ts +28 -0
- package/src/tools/builtin/shell.ts +43 -0
- package/src/tools/builtin/web.ts +35 -0
- package/src/tools/mcp-client.ts +131 -0
- package/src/types/agent-workstation.d.ts +2 -0
- package/tests/a2a-protocol.test.ts +285 -0
- package/tests/agui-protocol.test.ts +246 -0
- package/tests/auto-learn.test.ts +105 -0
- package/tests/builtin-tools.test.ts +83 -0
- package/tests/channels/discord.test.ts +79 -0
- package/tests/channels/email.test.ts +148 -0
- package/tests/channels/feishu.test.ts +123 -0
- package/tests/channels/telegram.test.ts +129 -0
- package/tests/channels/websocket.test.ts +53 -0
- package/tests/channels/wechat.test.ts +170 -0
- package/tests/chat-cli.test.ts +160 -0
- package/tests/cli.test.ts +46 -0
- package/tests/daemon.test.ts +135 -0
- package/tests/deepbrain-wire.test.ts +234 -0
- package/tests/doctor.test.ts +38 -0
- package/tests/eval.test.ts +173 -0
- package/tests/init-role.test.ts +124 -0
- package/tests/mcp-client.test.ts +92 -0
- package/tests/mcp-server.test.ts +178 -0
- package/tests/plugin-a2a-enhanced.test.ts +230 -0
- package/tests/publish.test.ts +231 -0
- package/tests/scheduler.test.ts +200 -0
- package/tests/security-enhanced.test.ts +233 -0
- package/tests/skill-learner.test.ts +161 -0
- package/tests/studio.test.ts +229 -0
- package/tests/subagent.test.ts +193 -0
- package/tests/telegram-discord.test.ts +60 -0
- package/tests/telemetry.test.ts +186 -0
- package/tests/tools/builtin-extended.test.ts +138 -0
- package/tests/workflow-graph.test.ts +279 -0
- package/tutorial/customer-service-agent/README.md +612 -0
- package/tutorial/customer-service-agent/SOUL.md +26 -0
- package/tutorial/customer-service-agent/agent.yaml +63 -0
- package/tutorial/customer-service-agent/package.json +19 -0
- package/tutorial/customer-service-agent/src/index.ts +69 -0
- package/tutorial/customer-service-agent/src/skills/faq.ts +27 -0
- package/tutorial/customer-service-agent/src/skills/ticket.ts +22 -0
- package/tutorial/customer-service-agent/tsconfig.json +14 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { createServer, type IncomingMessage, type ServerResponse } from 'http';
|
|
2
|
+
import { createInterface } from 'readline';
|
|
3
|
+
import type {
|
|
4
|
+
MCPServerConfig, MCPServerToolDefinition, MCPResourceDefinition,
|
|
5
|
+
MCPPromptDefinition, JsonRpcRequest, JsonRpcResponse,
|
|
6
|
+
} from './types';
|
|
7
|
+
import { MCP_ERRORS } from './types';
|
|
8
|
+
|
|
9
|
+
export class MCPServer {
|
|
10
|
+
private config: MCPServerConfig;
|
|
11
|
+
private tools: Map<string, MCPServerToolDefinition> = new Map();
|
|
12
|
+
private resources: Map<string, MCPResourceDefinition> = new Map();
|
|
13
|
+
private prompts: Map<string, MCPPromptDefinition> = new Map();
|
|
14
|
+
private sseClients: Set<ServerResponse> = new Set();
|
|
15
|
+
private httpServer: ReturnType<typeof createServer> | null = null;
|
|
16
|
+
|
|
17
|
+
constructor(config: MCPServerConfig) {
|
|
18
|
+
this.config = config;
|
|
19
|
+
for (const t of config.tools ?? []) this.addTool(t);
|
|
20
|
+
for (const r of config.resources ?? []) this.addResource(r);
|
|
21
|
+
for (const p of config.prompts ?? []) this.addPrompt(p);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
addTool(tool: MCPServerToolDefinition): void {
|
|
25
|
+
this.tools.set(tool.name, tool);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
removeTool(name: string): void {
|
|
29
|
+
this.tools.delete(name);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
addResource(resource: MCPResourceDefinition): void {
|
|
33
|
+
this.resources.set(resource.uri, resource);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
removeResource(uri: string): void {
|
|
37
|
+
this.resources.delete(uri);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
addPrompt(prompt: MCPPromptDefinition): void {
|
|
41
|
+
this.prompts.set(prompt.name, prompt);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
getToolCount(): number { return this.tools.size; }
|
|
45
|
+
getResourceCount(): number { return this.resources.size; }
|
|
46
|
+
getPromptCount(): number { return this.prompts.size; }
|
|
47
|
+
getConnectedClients(): number { return this.sseClients.size; }
|
|
48
|
+
|
|
49
|
+
/** Serve over stdio — one JSON-RPC message per line */
|
|
50
|
+
async serveStdio(): Promise<void> {
|
|
51
|
+
const rl = createInterface({ input: process.stdin, terminal: false });
|
|
52
|
+
rl.on('line', async (line) => {
|
|
53
|
+
const trimmed = line.trim();
|
|
54
|
+
if (!trimmed) return;
|
|
55
|
+
try {
|
|
56
|
+
const msg = JSON.parse(trimmed) as JsonRpcRequest;
|
|
57
|
+
const response = await this.handleMessage(msg);
|
|
58
|
+
if (response) {
|
|
59
|
+
process.stdout.write(JSON.stringify(response) + '\n');
|
|
60
|
+
}
|
|
61
|
+
} catch {
|
|
62
|
+
const err: JsonRpcResponse = {
|
|
63
|
+
jsonrpc: '2.0', id: null,
|
|
64
|
+
error: MCP_ERRORS.PARSE_ERROR,
|
|
65
|
+
};
|
|
66
|
+
process.stdout.write(JSON.stringify(err) + '\n');
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Serve over HTTP + SSE */
|
|
72
|
+
async serveHTTP(port: number): Promise<void> {
|
|
73
|
+
this.httpServer = createServer((req, res) => this.handleHTTP(req, res));
|
|
74
|
+
return new Promise((resolve) => {
|
|
75
|
+
this.httpServer!.listen(port, () => resolve());
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Mount on existing HTTP server at a path prefix */
|
|
80
|
+
mount(server: any, path = '/mcp'): void {
|
|
81
|
+
const orig = server.listeners('request')[0] as any;
|
|
82
|
+
server.removeAllListeners('request');
|
|
83
|
+
server.on('request', (req: IncomingMessage, res: ServerResponse) => {
|
|
84
|
+
if (req.url?.startsWith(path)) {
|
|
85
|
+
// Rewrite URL to strip prefix
|
|
86
|
+
req.url = req.url.slice(path.length) || '/';
|
|
87
|
+
this.handleHTTP(req, res);
|
|
88
|
+
} else if (orig) {
|
|
89
|
+
orig(req, res);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
stop(): void {
|
|
95
|
+
this.httpServer?.close();
|
|
96
|
+
for (const client of this.sseClients) {
|
|
97
|
+
client.end();
|
|
98
|
+
}
|
|
99
|
+
this.sseClients.clear();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private handleHTTP(req: IncomingMessage, res: ServerResponse): void {
|
|
103
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
104
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
105
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
106
|
+
|
|
107
|
+
if (req.method === 'OPTIONS') {
|
|
108
|
+
res.writeHead(204);
|
|
109
|
+
res.end();
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const url = new URL(req.url || '/', `http://${req.headers.host || 'localhost'}`);
|
|
114
|
+
|
|
115
|
+
if (url.pathname === '/sse' && req.method === 'GET') {
|
|
116
|
+
// SSE endpoint
|
|
117
|
+
res.writeHead(200, {
|
|
118
|
+
'Content-Type': 'text/event-stream',
|
|
119
|
+
'Cache-Control': 'no-cache',
|
|
120
|
+
'Connection': 'keep-alive',
|
|
121
|
+
});
|
|
122
|
+
this.sseClients.add(res);
|
|
123
|
+
// Send endpoint info
|
|
124
|
+
res.write(`data: ${JSON.stringify({ type: 'endpoint', url: '/message' })}\n\n`);
|
|
125
|
+
req.on('close', () => this.sseClients.delete(res));
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (url.pathname === '/message' && req.method === 'POST') {
|
|
130
|
+
let body = '';
|
|
131
|
+
req.on('data', (chunk) => { body += chunk; });
|
|
132
|
+
req.on('end', async () => {
|
|
133
|
+
try {
|
|
134
|
+
const msg = JSON.parse(body) as JsonRpcRequest;
|
|
135
|
+
const response = await this.handleMessage(msg);
|
|
136
|
+
if (response) {
|
|
137
|
+
// Send via SSE to all clients
|
|
138
|
+
for (const client of this.sseClients) {
|
|
139
|
+
client.write(`data: ${JSON.stringify(response)}\n\n`);
|
|
140
|
+
}
|
|
141
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
142
|
+
res.end(JSON.stringify(response));
|
|
143
|
+
} else {
|
|
144
|
+
res.writeHead(202);
|
|
145
|
+
res.end();
|
|
146
|
+
}
|
|
147
|
+
} catch {
|
|
148
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
149
|
+
res.end(JSON.stringify({ jsonrpc: '2.0', id: null, error: MCP_ERRORS.PARSE_ERROR }));
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
res.writeHead(404);
|
|
156
|
+
res.end('Not Found');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async handleMessage(msg: JsonRpcRequest): Promise<JsonRpcResponse | null> {
|
|
160
|
+
// Notifications (no id) don't get responses
|
|
161
|
+
if (msg.id === undefined) return null;
|
|
162
|
+
|
|
163
|
+
const id = msg.id;
|
|
164
|
+
try {
|
|
165
|
+
switch (msg.method) {
|
|
166
|
+
case 'initialize':
|
|
167
|
+
return this.rpcResult(id, {
|
|
168
|
+
protocolVersion: '2024-11-05',
|
|
169
|
+
capabilities: {
|
|
170
|
+
tools: this.tools.size > 0 ? { listChanged: true } : undefined,
|
|
171
|
+
resources: this.resources.size > 0 ? { subscribe: false, listChanged: true } : undefined,
|
|
172
|
+
prompts: this.prompts.size > 0 ? { listChanged: true } : undefined,
|
|
173
|
+
},
|
|
174
|
+
serverInfo: { name: this.config.name, version: this.config.version },
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
case 'tools/list':
|
|
178
|
+
return this.rpcResult(id, {
|
|
179
|
+
tools: Array.from(this.tools.values()).map(t => ({
|
|
180
|
+
name: t.name,
|
|
181
|
+
description: t.description,
|
|
182
|
+
inputSchema: t.inputSchema,
|
|
183
|
+
})),
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
case 'tools/call': {
|
|
187
|
+
const { name, arguments: args } = msg.params ?? {};
|
|
188
|
+
const tool = this.tools.get(name);
|
|
189
|
+
if (!tool) return this.rpcError(id, MCP_ERRORS.TOOL_NOT_FOUND);
|
|
190
|
+
// Validate required fields
|
|
191
|
+
const schema = tool.inputSchema;
|
|
192
|
+
if (schema?.required && Array.isArray(schema.required)) {
|
|
193
|
+
for (const field of schema.required) {
|
|
194
|
+
if (args?.[field] === undefined) {
|
|
195
|
+
return this.rpcError(id, { code: -32602, message: `Missing required parameter: ${field}` });
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
try {
|
|
200
|
+
const result = await tool.handler(args ?? {});
|
|
201
|
+
return this.rpcResult(id, {
|
|
202
|
+
content: [{ type: 'text', text: typeof result === 'string' ? result : JSON.stringify(result) }],
|
|
203
|
+
});
|
|
204
|
+
} catch (err: any) {
|
|
205
|
+
return this.rpcResult(id, {
|
|
206
|
+
content: [{ type: 'text', text: err.message || 'Tool execution failed' }],
|
|
207
|
+
isError: true,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
case 'resources/list':
|
|
213
|
+
return this.rpcResult(id, {
|
|
214
|
+
resources: Array.from(this.resources.values()).map(r => ({
|
|
215
|
+
uri: r.uri, name: r.name, description: r.description, mimeType: r.mimeType,
|
|
216
|
+
})),
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
case 'resources/read': {
|
|
220
|
+
const { uri } = msg.params ?? {};
|
|
221
|
+
const resource = this.resources.get(uri);
|
|
222
|
+
if (!resource) return this.rpcError(id, MCP_ERRORS.RESOURCE_NOT_FOUND);
|
|
223
|
+
const content = await resource.handler();
|
|
224
|
+
return this.rpcResult(id, {
|
|
225
|
+
contents: [{ uri, mimeType: resource.mimeType || 'text/plain', text: content }],
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
case 'prompts/list':
|
|
230
|
+
return this.rpcResult(id, {
|
|
231
|
+
prompts: Array.from(this.prompts.values()).map(p => ({
|
|
232
|
+
name: p.name, description: p.description,
|
|
233
|
+
arguments: p.arguments,
|
|
234
|
+
})),
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
case 'prompts/get': {
|
|
238
|
+
const { name, arguments: promptArgs } = msg.params ?? {};
|
|
239
|
+
const prompt = this.prompts.get(name);
|
|
240
|
+
if (!prompt) return this.rpcError(id, MCP_ERRORS.PROMPT_NOT_FOUND);
|
|
241
|
+
const messages = prompt.handler
|
|
242
|
+
? await prompt.handler(promptArgs ?? {})
|
|
243
|
+
: [{ role: 'user' as const, content: { type: 'text' as const, text: prompt.description || '' } }];
|
|
244
|
+
return this.rpcResult(id, { description: prompt.description, messages });
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
default:
|
|
248
|
+
return this.rpcError(id, MCP_ERRORS.METHOD_NOT_FOUND);
|
|
249
|
+
}
|
|
250
|
+
} catch (err: any) {
|
|
251
|
+
return this.rpcError(id, { ...MCP_ERRORS.INTERNAL_ERROR, data: err.message });
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
private rpcResult(id: number | string, result: any): JsonRpcResponse {
|
|
256
|
+
return { jsonrpc: '2.0', id, result };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private rpcError(id: number | string, error: { code: number; message: string; data?: any }): JsonRpcResponse {
|
|
260
|
+
return { jsonrpc: '2.0', id, error };
|
|
261
|
+
}
|
|
262
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Server types — JSON-RPC 2.0 based Model Context Protocol
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export interface MCPServerConfig {
|
|
6
|
+
name: string;
|
|
7
|
+
version: string;
|
|
8
|
+
tools?: MCPServerToolDefinition[];
|
|
9
|
+
resources?: MCPResourceDefinition[];
|
|
10
|
+
prompts?: MCPPromptDefinition[];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface MCPServerToolDefinition {
|
|
14
|
+
name: string;
|
|
15
|
+
description: string;
|
|
16
|
+
inputSchema: Record<string, any>;
|
|
17
|
+
handler: (args: any) => Promise<any>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface MCPResourceDefinition {
|
|
21
|
+
uri: string;
|
|
22
|
+
name: string;
|
|
23
|
+
description?: string;
|
|
24
|
+
mimeType?: string;
|
|
25
|
+
handler: () => Promise<string>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface MCPPromptDefinition {
|
|
29
|
+
name: string;
|
|
30
|
+
description?: string;
|
|
31
|
+
arguments?: MCPPromptArgument[];
|
|
32
|
+
handler?: (args: Record<string, string>) => Promise<MCPPromptMessage[]>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface MCPPromptArgument {
|
|
36
|
+
name: string;
|
|
37
|
+
description?: string;
|
|
38
|
+
required?: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface MCPPromptMessage {
|
|
42
|
+
role: 'user' | 'assistant';
|
|
43
|
+
content: { type: 'text'; text: string };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface JsonRpcRequest {
|
|
47
|
+
jsonrpc: '2.0';
|
|
48
|
+
id?: number | string;
|
|
49
|
+
method: string;
|
|
50
|
+
params?: any;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface JsonRpcResponse {
|
|
54
|
+
jsonrpc: '2.0';
|
|
55
|
+
id: number | string | null;
|
|
56
|
+
result?: any;
|
|
57
|
+
error?: { code: number; message: string; data?: any };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const MCP_ERRORS = {
|
|
61
|
+
PARSE_ERROR: { code: -32700, message: 'Parse error' },
|
|
62
|
+
INVALID_REQUEST: { code: -32600, message: 'Invalid Request' },
|
|
63
|
+
METHOD_NOT_FOUND: { code: -32601, message: 'Method not found' },
|
|
64
|
+
INVALID_PARAMS: { code: -32602, message: 'Invalid params' },
|
|
65
|
+
INTERNAL_ERROR: { code: -32603, message: 'Internal error' },
|
|
66
|
+
TOOL_NOT_FOUND: { code: -32001, message: 'Tool not found' },
|
|
67
|
+
RESOURCE_NOT_FOUND: { code: -32002, message: 'Resource not found' },
|
|
68
|
+
PROMPT_NOT_FOUND: { code: -32003, message: 'Prompt not found' },
|
|
69
|
+
} as const;
|