@cogitator-ai/mcp 1.1.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +627 -30
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -8,73 +8,670 @@ MCP (Model Context Protocol) integration for Cogitator. Connect to external MCP
|
|
|
8
8
|
pnpm add @cogitator-ai/mcp
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## Features
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
- **MCP Client** - Connect to any MCP server (stdio, HTTP, SSE)
|
|
14
|
+
- **MCP Server** - Expose Cogitator tools as MCP endpoints
|
|
15
|
+
- **Tool Adapters** - Bidirectional conversion between Cogitator and MCP formats
|
|
16
|
+
- **Schema Converters** - Convert between Zod and JSON Schema
|
|
17
|
+
- **Resources & Prompts** - Access MCP resources and prompt templates
|
|
18
|
+
- **Transport Flexibility** - Support for stdio, HTTP, and SSE transports
|
|
14
19
|
|
|
15
|
-
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
### Use Tools from an MCP Server
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { MCPClient } from '@cogitator-ai/mcp';
|
|
28
|
+
import { Agent, Cogitator } from '@cogitator-ai/core';
|
|
29
|
+
|
|
30
|
+
const client = await MCPClient.connect({
|
|
31
|
+
transport: 'stdio',
|
|
32
|
+
command: 'npx',
|
|
33
|
+
args: ['-y', '@anthropic/mcp-server-filesystem', '/allowed/path'],
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const tools = await client.getTools();
|
|
37
|
+
|
|
38
|
+
const agent = new Agent({
|
|
39
|
+
name: 'File Agent',
|
|
40
|
+
model: 'ollama/llama3.1:8b',
|
|
41
|
+
instructions: 'You can read and write files.',
|
|
42
|
+
tools,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const cog = new Cogitator();
|
|
46
|
+
const result = await cog.run(agent, {
|
|
47
|
+
input: 'List files in /allowed/path',
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
await client.close();
|
|
51
|
+
await cog.close();
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Expose Cogitator Tools as MCP Server
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { MCPServer } from '@cogitator-ai/mcp';
|
|
58
|
+
import { tool } from '@cogitator-ai/core';
|
|
59
|
+
import { z } from 'zod';
|
|
60
|
+
|
|
61
|
+
const calculator = tool({
|
|
62
|
+
name: 'calculator',
|
|
63
|
+
description: 'Perform math calculations',
|
|
64
|
+
parameters: z.object({
|
|
65
|
+
expression: z.string().describe('Math expression'),
|
|
66
|
+
}),
|
|
67
|
+
execute: async ({ expression }) => {
|
|
68
|
+
return String(eval(expression));
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const server = new MCPServer({
|
|
73
|
+
name: 'my-tools',
|
|
74
|
+
version: '1.0.0',
|
|
75
|
+
transport: 'stdio',
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
server.registerTool(calculator);
|
|
79
|
+
await server.start();
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## MCP Client
|
|
85
|
+
|
|
86
|
+
Connect to external MCP servers and use their tools with Cogitator agents.
|
|
87
|
+
|
|
88
|
+
### Connection Options
|
|
16
89
|
|
|
17
90
|
```typescript
|
|
18
91
|
import { MCPClient } from '@cogitator-ai/mcp';
|
|
19
92
|
|
|
20
|
-
const client =
|
|
93
|
+
const client = await MCPClient.connect({
|
|
21
94
|
transport: 'stdio',
|
|
22
95
|
command: 'npx',
|
|
23
|
-
args: ['-y', '@anthropic/mcp-server-filesystem'],
|
|
96
|
+
args: ['-y', '@anthropic/mcp-server-filesystem', '/path'],
|
|
97
|
+
env: { DEBUG: 'true' },
|
|
98
|
+
timeout: 30000,
|
|
99
|
+
clientName: 'my-app',
|
|
100
|
+
clientVersion: '1.0.0',
|
|
24
101
|
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Configuration
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
interface MCPClientConfig {
|
|
108
|
+
transport: 'stdio' | 'http' | 'sse';
|
|
109
|
+
|
|
110
|
+
// For stdio transport
|
|
111
|
+
command?: string;
|
|
112
|
+
args?: string[];
|
|
113
|
+
env?: Record<string, string>;
|
|
114
|
+
|
|
115
|
+
// For HTTP/SSE transport
|
|
116
|
+
url?: string;
|
|
117
|
+
|
|
118
|
+
// Connection options
|
|
119
|
+
timeout?: number;
|
|
120
|
+
clientName?: string;
|
|
121
|
+
clientVersion?: string;
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Transport Types
|
|
126
|
+
|
|
127
|
+
| Transport | Use Case |
|
|
128
|
+
| --------- | -------------------------------------------- |
|
|
129
|
+
| `stdio` | Local MCP servers spawned as child processes |
|
|
130
|
+
| `http` | Remote MCP servers over HTTP |
|
|
131
|
+
| `sse` | Server-Sent Events for streaming |
|
|
132
|
+
|
|
133
|
+
### Client Methods
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
const client = await MCPClient.connect(config);
|
|
137
|
+
|
|
138
|
+
client.isConnected();
|
|
139
|
+
|
|
140
|
+
client.getCapabilities();
|
|
141
|
+
|
|
142
|
+
const definitions = await client.listToolDefinitions();
|
|
143
|
+
|
|
144
|
+
const tools = await client.getTools();
|
|
145
|
+
|
|
146
|
+
const result = await client.callTool('tool_name', { arg: 'value' });
|
|
147
|
+
|
|
148
|
+
const resources = await client.listResources();
|
|
149
|
+
|
|
150
|
+
const content = await client.readResource('file://path/to/file');
|
|
151
|
+
|
|
152
|
+
const prompts = await client.listPrompts();
|
|
153
|
+
|
|
154
|
+
const messages = await client.getPrompt('prompt_name', { arg: 'value' });
|
|
155
|
+
|
|
156
|
+
await client.close();
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Helper Function
|
|
25
160
|
|
|
26
|
-
|
|
161
|
+
For quick one-liner connections:
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
import { connectMCPServer } from '@cogitator-ai/mcp';
|
|
27
165
|
|
|
28
|
-
|
|
29
|
-
|
|
166
|
+
const { tools, client, cleanup } = await connectMCPServer({
|
|
167
|
+
transport: 'stdio',
|
|
168
|
+
command: 'npx',
|
|
169
|
+
args: ['-y', '@anthropic/mcp-server-filesystem', '/path'],
|
|
170
|
+
});
|
|
30
171
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
172
|
+
const agent = new Agent({
|
|
173
|
+
tools,
|
|
174
|
+
// ...
|
|
34
175
|
});
|
|
35
176
|
|
|
36
|
-
//
|
|
37
|
-
|
|
177
|
+
// When done
|
|
178
|
+
await cleanup();
|
|
38
179
|
```
|
|
39
180
|
|
|
40
|
-
|
|
181
|
+
---
|
|
41
182
|
|
|
42
|
-
|
|
183
|
+
## MCP Server
|
|
184
|
+
|
|
185
|
+
Expose Cogitator tools as an MCP server for use by Claude Desktop, other AI assistants, or any MCP client.
|
|
186
|
+
|
|
187
|
+
### Creating a Server
|
|
43
188
|
|
|
44
189
|
```typescript
|
|
45
190
|
import { MCPServer } from '@cogitator-ai/mcp';
|
|
46
|
-
import { builtinTools } from '@cogitator-ai/core';
|
|
47
191
|
|
|
48
192
|
const server = new MCPServer({
|
|
193
|
+
name: 'my-cogitator-server',
|
|
194
|
+
version: '1.0.0',
|
|
195
|
+
transport: 'stdio',
|
|
196
|
+
logging: true,
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
server.registerTool(tool1);
|
|
200
|
+
server.registerTool(tool2);
|
|
201
|
+
// or
|
|
202
|
+
server.registerTools([tool1, tool2, tool3]);
|
|
203
|
+
|
|
204
|
+
await server.start();
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Server Configuration
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
interface MCPServerConfig {
|
|
211
|
+
name: string;
|
|
212
|
+
version: string;
|
|
213
|
+
transport: 'stdio' | 'http' | 'sse';
|
|
214
|
+
|
|
215
|
+
// For HTTP transport
|
|
216
|
+
port?: number; // Default: 3000
|
|
217
|
+
host?: string; // Default: 'localhost'
|
|
218
|
+
|
|
219
|
+
logging?: boolean; // Enable console logging
|
|
220
|
+
}
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### Server Methods
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
const server = new MCPServer(config);
|
|
227
|
+
|
|
228
|
+
server.registerTool(tool);
|
|
229
|
+
|
|
230
|
+
server.registerTools([tool1, tool2]);
|
|
231
|
+
|
|
232
|
+
server.unregisterTool('tool_name');
|
|
233
|
+
|
|
234
|
+
server.getRegisteredTools();
|
|
235
|
+
|
|
236
|
+
await server.start();
|
|
237
|
+
|
|
238
|
+
server.isRunning();
|
|
239
|
+
|
|
240
|
+
await server.stop();
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### HTTP Server
|
|
244
|
+
|
|
245
|
+
Run MCP over HTTP:
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
const server = new MCPServer({
|
|
249
|
+
name: 'http-tools',
|
|
250
|
+
version: '1.0.0',
|
|
251
|
+
transport: 'http',
|
|
252
|
+
port: 3001,
|
|
253
|
+
host: '0.0.0.0',
|
|
254
|
+
logging: true,
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
server.registerTools(myTools);
|
|
258
|
+
await server.start();
|
|
259
|
+
// Server listening on http://0.0.0.0:3001/mcp
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### Stdio Server (for Claude Desktop)
|
|
263
|
+
|
|
264
|
+
Create a script that Claude Desktop can execute:
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
// serve-tools.ts
|
|
268
|
+
import { serveMCPTools } from '@cogitator-ai/mcp';
|
|
269
|
+
import { builtinTools } from '@cogitator-ai/core';
|
|
270
|
+
|
|
271
|
+
await serveMCPTools(builtinTools, {
|
|
49
272
|
name: 'cogitator-tools',
|
|
50
273
|
version: '1.0.0',
|
|
51
|
-
|
|
274
|
+
transport: 'stdio',
|
|
275
|
+
});
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
Add to Claude Desktop config:
|
|
279
|
+
|
|
280
|
+
```json
|
|
281
|
+
{
|
|
282
|
+
"mcpServers": {
|
|
283
|
+
"cogitator": {
|
|
284
|
+
"command": "npx",
|
|
285
|
+
"args": ["tsx", "/path/to/serve-tools.ts"]
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
---
|
|
292
|
+
|
|
293
|
+
## Tool Adapters
|
|
294
|
+
|
|
295
|
+
Convert between Cogitator and MCP tool formats.
|
|
296
|
+
|
|
297
|
+
### Cogitator → MCP
|
|
298
|
+
|
|
299
|
+
```typescript
|
|
300
|
+
import { cogitatorToMCP } from '@cogitator-ai/mcp';
|
|
301
|
+
import { tool } from '@cogitator-ai/core';
|
|
302
|
+
import { z } from 'zod';
|
|
303
|
+
|
|
304
|
+
const myTool = tool({
|
|
305
|
+
name: 'greet',
|
|
306
|
+
description: 'Greet someone',
|
|
307
|
+
parameters: z.object({
|
|
308
|
+
name: z.string(),
|
|
309
|
+
}),
|
|
310
|
+
execute: async ({ name }) => `Hello, ${name}!`,
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
const mcpDefinition = cogitatorToMCP(myTool);
|
|
314
|
+
// {
|
|
315
|
+
// name: 'greet',
|
|
316
|
+
// description: 'Greet someone',
|
|
317
|
+
// inputSchema: {
|
|
318
|
+
// type: 'object',
|
|
319
|
+
// properties: { name: { type: 'string' } },
|
|
320
|
+
// required: ['name']
|
|
321
|
+
// }
|
|
322
|
+
// }
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
### MCP → Cogitator
|
|
326
|
+
|
|
327
|
+
```typescript
|
|
328
|
+
import { mcpToCogitator, wrapMCPTools } from '@cogitator-ai/mcp';
|
|
329
|
+
|
|
330
|
+
// Single tool
|
|
331
|
+
const cogitatorTool = mcpToCogitator(mcpToolDefinition, mcpClient, {
|
|
332
|
+
namePrefix: 'mcp_',
|
|
333
|
+
descriptionTransform: (desc) => `[MCP] ${desc}`,
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
// All tools from a client
|
|
337
|
+
const tools = await wrapMCPTools(client, {
|
|
338
|
+
namePrefix: 'fs_',
|
|
339
|
+
});
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
### Adapter Options
|
|
343
|
+
|
|
344
|
+
```typescript
|
|
345
|
+
interface ToolAdapterOptions {
|
|
346
|
+
namePrefix?: string;
|
|
347
|
+
descriptionTransform?: (description: string) => string;
|
|
348
|
+
}
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
---
|
|
352
|
+
|
|
353
|
+
## Schema Converters
|
|
354
|
+
|
|
355
|
+
Convert between Zod schemas and JSON Schema.
|
|
356
|
+
|
|
357
|
+
### Zod → JSON Schema
|
|
358
|
+
|
|
359
|
+
```typescript
|
|
360
|
+
import { zodToJsonSchema } from '@cogitator-ai/mcp';
|
|
361
|
+
import { z } from 'zod';
|
|
362
|
+
|
|
363
|
+
const schema = z.object({
|
|
364
|
+
name: z.string().min(1).describe('User name'),
|
|
365
|
+
age: z.number().int().min(0).optional(),
|
|
366
|
+
email: z.string().email(),
|
|
367
|
+
role: z.enum(['admin', 'user', 'guest']),
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
const jsonSchema = zodToJsonSchema(schema);
|
|
371
|
+
// {
|
|
372
|
+
// type: 'object',
|
|
373
|
+
// properties: {
|
|
374
|
+
// name: { type: 'string', minLength: 1, description: 'User name' },
|
|
375
|
+
// age: { type: 'integer', minimum: 0 },
|
|
376
|
+
// email: { type: 'string', format: 'email' },
|
|
377
|
+
// role: { type: 'string', enum: ['admin', 'user', 'guest'] }
|
|
378
|
+
// },
|
|
379
|
+
// required: ['name', 'email', 'role']
|
|
380
|
+
// }
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
### JSON Schema → Zod
|
|
384
|
+
|
|
385
|
+
```typescript
|
|
386
|
+
import { jsonSchemaToZod } from '@cogitator-ai/mcp';
|
|
387
|
+
|
|
388
|
+
const jsonSchema = {
|
|
389
|
+
type: 'object',
|
|
390
|
+
properties: {
|
|
391
|
+
query: { type: 'string', description: 'Search query' },
|
|
392
|
+
limit: { type: 'integer', minimum: 1, maximum: 100 },
|
|
393
|
+
tags: { type: 'array', items: { type: 'string' } },
|
|
394
|
+
},
|
|
395
|
+
required: ['query'],
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
const zodSchema = jsonSchemaToZod(jsonSchema);
|
|
399
|
+
|
|
400
|
+
const result = zodSchema.parse({
|
|
401
|
+
query: 'test',
|
|
402
|
+
limit: 10,
|
|
403
|
+
tags: ['a', 'b'],
|
|
404
|
+
});
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
### Supported Conversions
|
|
408
|
+
|
|
409
|
+
| JSON Schema | Zod |
|
|
410
|
+
| -------------------------------- | ------------------------ |
|
|
411
|
+
| `string` | `z.string()` |
|
|
412
|
+
| `string` + `minLength/maxLength` | `z.string().min().max()` |
|
|
413
|
+
| `string` + `pattern` | `z.string().regex()` |
|
|
414
|
+
| `string` + `format: email` | `z.string().email()` |
|
|
415
|
+
| `string` + `format: uri` | `z.string().url()` |
|
|
416
|
+
| `number` | `z.number()` |
|
|
417
|
+
| `integer` | `z.number().int()` |
|
|
418
|
+
| `number` + `minimum/maximum` | `z.number().min().max()` |
|
|
419
|
+
| `boolean` | `z.boolean()` |
|
|
420
|
+
| `array` | `z.array()` |
|
|
421
|
+
| `object` | `z.object()` |
|
|
422
|
+
| `null` | `z.null()` |
|
|
423
|
+
| `enum` | `z.enum()` |
|
|
424
|
+
|
|
425
|
+
---
|
|
426
|
+
|
|
427
|
+
## Resources
|
|
428
|
+
|
|
429
|
+
Access data from MCP servers that expose resources.
|
|
430
|
+
|
|
431
|
+
```typescript
|
|
432
|
+
const client = await MCPClient.connect(config);
|
|
433
|
+
|
|
434
|
+
const resources = await client.listResources();
|
|
435
|
+
// [
|
|
436
|
+
// { uri: 'file:///path/to/file.txt', name: 'file.txt', mimeType: 'text/plain' },
|
|
437
|
+
// { uri: 'config://settings', name: 'Settings', description: 'App settings' },
|
|
438
|
+
// ]
|
|
439
|
+
|
|
440
|
+
const content = await client.readResource('file:///path/to/file.txt');
|
|
441
|
+
// {
|
|
442
|
+
// uri: 'file:///path/to/file.txt',
|
|
443
|
+
// mimeType: 'text/plain',
|
|
444
|
+
// text: 'File contents here...',
|
|
445
|
+
// }
|
|
446
|
+
|
|
447
|
+
if (content.blob) {
|
|
448
|
+
const data = Buffer.from(content.blob, 'base64');
|
|
449
|
+
}
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
### Resource Types
|
|
453
|
+
|
|
454
|
+
```typescript
|
|
455
|
+
interface MCPResource {
|
|
456
|
+
uri: string;
|
|
457
|
+
name: string;
|
|
458
|
+
description?: string;
|
|
459
|
+
mimeType?: string;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
interface MCPResourceContent {
|
|
463
|
+
uri: string;
|
|
464
|
+
mimeType?: string;
|
|
465
|
+
text?: string;
|
|
466
|
+
blob?: string; // Base64 encoded
|
|
467
|
+
}
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
---
|
|
471
|
+
|
|
472
|
+
## Prompts
|
|
473
|
+
|
|
474
|
+
Access prompt templates from MCP servers.
|
|
475
|
+
|
|
476
|
+
```typescript
|
|
477
|
+
const client = await MCPClient.connect(config);
|
|
478
|
+
|
|
479
|
+
const prompts = await client.listPrompts();
|
|
480
|
+
// [
|
|
481
|
+
// {
|
|
482
|
+
// name: 'code_review',
|
|
483
|
+
// description: 'Review code for issues',
|
|
484
|
+
// arguments: [
|
|
485
|
+
// { name: 'code', required: true },
|
|
486
|
+
// { name: 'language', required: false }
|
|
487
|
+
// ]
|
|
488
|
+
// }
|
|
489
|
+
// ]
|
|
490
|
+
|
|
491
|
+
const messages = await client.getPrompt('code_review', {
|
|
492
|
+
code: 'function add(a, b) { return a + b; }',
|
|
493
|
+
language: 'javascript',
|
|
494
|
+
});
|
|
495
|
+
// [
|
|
496
|
+
// {
|
|
497
|
+
// role: 'user',
|
|
498
|
+
// content: { type: 'text', text: 'Please review this JavaScript code...' }
|
|
499
|
+
// }
|
|
500
|
+
// ]
|
|
501
|
+
```
|
|
502
|
+
|
|
503
|
+
### Prompt Types
|
|
504
|
+
|
|
505
|
+
```typescript
|
|
506
|
+
interface MCPPrompt {
|
|
507
|
+
name: string;
|
|
508
|
+
description?: string;
|
|
509
|
+
arguments?: MCPPromptArgument[];
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
interface MCPPromptArgument {
|
|
513
|
+
name: string;
|
|
514
|
+
description?: string;
|
|
515
|
+
required?: boolean;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
interface MCPPromptMessage {
|
|
519
|
+
role: 'user' | 'assistant';
|
|
520
|
+
content: {
|
|
521
|
+
type: 'text' | 'image' | 'resource';
|
|
522
|
+
text?: string;
|
|
523
|
+
data?: string;
|
|
524
|
+
mimeType?: string;
|
|
525
|
+
resource?: { uri: string; text?: string; blob?: string };
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
---
|
|
531
|
+
|
|
532
|
+
## Examples
|
|
533
|
+
|
|
534
|
+
### Multi-Server Integration
|
|
535
|
+
|
|
536
|
+
Use tools from multiple MCP servers:
|
|
537
|
+
|
|
538
|
+
```typescript
|
|
539
|
+
import { MCPClient, wrapMCPTools } from '@cogitator-ai/mcp';
|
|
540
|
+
import { Agent, Cogitator } from '@cogitator-ai/core';
|
|
541
|
+
|
|
542
|
+
const fsClient = await MCPClient.connect({
|
|
543
|
+
transport: 'stdio',
|
|
544
|
+
command: 'npx',
|
|
545
|
+
args: ['-y', '@anthropic/mcp-server-filesystem', '/workspace'],
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
const gitClient = await MCPClient.connect({
|
|
549
|
+
transport: 'stdio',
|
|
550
|
+
command: 'npx',
|
|
551
|
+
args: ['-y', '@anthropic/mcp-server-git'],
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
const fsTools = await wrapMCPTools(fsClient, { namePrefix: 'fs_' });
|
|
555
|
+
const gitTools = await wrapMCPTools(gitClient, { namePrefix: 'git_' });
|
|
556
|
+
|
|
557
|
+
const agent = new Agent({
|
|
558
|
+
name: 'Dev Assistant',
|
|
559
|
+
model: 'ollama/llama3.1:8b',
|
|
560
|
+
instructions: 'You can manage files and git repositories.',
|
|
561
|
+
tools: [...fsTools, ...gitTools],
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
const cog = new Cogitator();
|
|
565
|
+
const result = await cog.run(agent, {
|
|
566
|
+
input: 'Create a new file called hello.ts and commit it',
|
|
52
567
|
});
|
|
53
568
|
|
|
54
|
-
|
|
55
|
-
await
|
|
569
|
+
await fsClient.close();
|
|
570
|
+
await gitClient.close();
|
|
571
|
+
await cog.close();
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
### Capability-Based Tool Selection
|
|
575
|
+
|
|
576
|
+
Check server capabilities before using features:
|
|
577
|
+
|
|
578
|
+
```typescript
|
|
579
|
+
const client = await MCPClient.connect(config);
|
|
580
|
+
const capabilities = client.getCapabilities();
|
|
581
|
+
|
|
582
|
+
const tools: Tool[] = [];
|
|
583
|
+
|
|
584
|
+
if (capabilities.tools) {
|
|
585
|
+
tools.push(...(await client.getTools()));
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
if (capabilities.resources) {
|
|
589
|
+
const resources = await client.listResources();
|
|
590
|
+
console.log(`Available resources: ${resources.length}`);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
if (capabilities.prompts) {
|
|
594
|
+
const prompts = await client.listPrompts();
|
|
595
|
+
console.log(`Available prompts: ${prompts.length}`);
|
|
596
|
+
}
|
|
597
|
+
```
|
|
598
|
+
|
|
599
|
+
### Error Handling
|
|
56
600
|
|
|
57
|
-
|
|
58
|
-
|
|
601
|
+
```typescript
|
|
602
|
+
import { MCPClient } from '@cogitator-ai/mcp';
|
|
603
|
+
|
|
604
|
+
try {
|
|
605
|
+
const client = await MCPClient.connect({
|
|
606
|
+
transport: 'stdio',
|
|
607
|
+
command: 'nonexistent-command',
|
|
608
|
+
timeout: 5000,
|
|
609
|
+
});
|
|
610
|
+
} catch (error) {
|
|
611
|
+
if (error.message.includes('timeout')) {
|
|
612
|
+
console.error('Connection timed out');
|
|
613
|
+
} else if (error.message.includes('ENOENT')) {
|
|
614
|
+
console.error('Command not found');
|
|
615
|
+
} else {
|
|
616
|
+
console.error('Connection failed:', error.message);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
59
619
|
```
|
|
60
620
|
|
|
61
|
-
###
|
|
621
|
+
### Content Conversion
|
|
62
622
|
|
|
63
|
-
|
|
623
|
+
Handle different content types from tool results:
|
|
64
624
|
|
|
65
625
|
```typescript
|
|
66
|
-
import {
|
|
626
|
+
import { resultToMCPContent, mcpContentToResult } from '@cogitator-ai/mcp';
|
|
627
|
+
|
|
628
|
+
const result = { data: [1, 2, 3], status: 'ok' };
|
|
629
|
+
const mcpContent = resultToMCPContent(result);
|
|
630
|
+
// [{ type: 'text', text: '{"data":[1,2,3],"status":"ok"}' }]
|
|
67
631
|
|
|
68
|
-
|
|
69
|
-
|
|
632
|
+
const parsed = mcpContentToResult(mcpContent);
|
|
633
|
+
// { data: [1, 2, 3], status: 'ok' }
|
|
70
634
|
|
|
71
|
-
|
|
72
|
-
|
|
635
|
+
const textResult = resultToMCPContent('Hello world');
|
|
636
|
+
// [{ type: 'text', text: 'Hello world' }]
|
|
73
637
|
```
|
|
74
638
|
|
|
75
|
-
|
|
639
|
+
---
|
|
640
|
+
|
|
641
|
+
## Type Reference
|
|
642
|
+
|
|
643
|
+
```typescript
|
|
644
|
+
import type {
|
|
645
|
+
// Transport
|
|
646
|
+
MCPTransportType,
|
|
647
|
+
|
|
648
|
+
// Client
|
|
649
|
+
MCPClientConfig,
|
|
650
|
+
|
|
651
|
+
// Server
|
|
652
|
+
MCPServerConfig,
|
|
653
|
+
|
|
654
|
+
// Tools
|
|
655
|
+
MCPToolDefinition,
|
|
656
|
+
MCPToolCallResult,
|
|
657
|
+
MCPToolContent,
|
|
658
|
+
|
|
659
|
+
// Resources
|
|
660
|
+
MCPResource,
|
|
661
|
+
MCPResourceContent,
|
|
662
|
+
|
|
663
|
+
// Prompts
|
|
664
|
+
MCPPrompt,
|
|
665
|
+
MCPPromptArgument,
|
|
666
|
+
MCPPromptMessage,
|
|
667
|
+
|
|
668
|
+
// Adapters
|
|
669
|
+
ToolAdapterOptions,
|
|
670
|
+
ConvertedTools,
|
|
671
|
+
} from '@cogitator-ai/mcp';
|
|
672
|
+
```
|
|
76
673
|
|
|
77
|
-
|
|
674
|
+
---
|
|
78
675
|
|
|
79
676
|
## License
|
|
80
677
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cogitator-ai/mcp",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "MCP (Model Context Protocol) integration for Cogitator",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -19,14 +19,14 @@
|
|
|
19
19
|
"@types/node": "^25.0.0",
|
|
20
20
|
"zod": "^3.22.4",
|
|
21
21
|
"zod-to-json-schema": "^3.24.5",
|
|
22
|
-
"@cogitator-ai/types": "0.
|
|
22
|
+
"@cogitator-ai/types": "0.4.0"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"typescript": "^5.3.0",
|
|
26
26
|
"vitest": "^1.6.0"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
|
-
"@cogitator-ai/types": "0.
|
|
29
|
+
"@cogitator-ai/types": "0.4.0"
|
|
30
30
|
},
|
|
31
31
|
"engines": {
|
|
32
32
|
"node": ">=20.0.0"
|