@rudderjs/ai 1.5.0 → 1.6.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 +399 -0
- package/boost/guidelines.md +60 -0
- package/dist/agent.d.ts +35 -1
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +118 -16
- package/dist/agent.js.map +1 -1
- package/dist/budget/pricing.d.ts +124 -0
- package/dist/budget/pricing.d.ts.map +1 -0
- package/dist/budget/pricing.js +175 -0
- package/dist/budget/pricing.js.map +1 -0
- package/dist/budget/storage.d.ts +104 -0
- package/dist/budget/storage.d.ts.map +1 -0
- package/dist/budget/storage.js +0 -0
- package/dist/budget/storage.js.map +1 -0
- package/dist/budget/with-budget.d.ts +119 -0
- package/dist/budget/with-budget.d.ts.map +1 -0
- package/dist/budget/with-budget.js +175 -0
- package/dist/budget/with-budget.js.map +1 -0
- package/dist/budget-orm/index.d.ts +96 -0
- package/dist/budget-orm/index.d.ts.map +1 -0
- package/dist/budget-orm/index.js +177 -0
- package/dist/budget-orm/index.js.map +1 -0
- package/dist/commands/ai-eval.d.ts +93 -0
- package/dist/commands/ai-eval.d.ts.map +1 -0
- package/dist/commands/ai-eval.js +378 -0
- package/dist/commands/ai-eval.js.map +1 -0
- package/dist/computer-use/actions.d.ts +214 -0
- package/dist/computer-use/actions.d.ts.map +1 -0
- package/dist/computer-use/actions.js +48 -0
- package/dist/computer-use/actions.js.map +1 -0
- package/dist/computer-use/errors.d.ts +57 -0
- package/dist/computer-use/errors.d.ts.map +1 -0
- package/dist/computer-use/errors.js +76 -0
- package/dist/computer-use/errors.js.map +1 -0
- package/dist/computer-use/index.d.ts +53 -0
- package/dist/computer-use/index.d.ts.map +1 -0
- package/dist/computer-use/index.js +51 -0
- package/dist/computer-use/index.js.map +1 -0
- package/dist/computer-use/playwright.d.ts +76 -0
- package/dist/computer-use/playwright.d.ts.map +1 -0
- package/dist/computer-use/playwright.js +270 -0
- package/dist/computer-use/playwright.js.map +1 -0
- package/dist/computer-use/tool.d.ts +154 -0
- package/dist/computer-use/tool.d.ts.map +1 -0
- package/dist/computer-use/tool.js +210 -0
- package/dist/computer-use/tool.js.map +1 -0
- package/dist/eval/fixtures.d.ts +65 -0
- package/dist/eval/fixtures.d.ts.map +1 -0
- package/dist/eval/fixtures.js +110 -0
- package/dist/eval/fixtures.js.map +1 -0
- package/dist/eval/html-reporter.d.ts +25 -0
- package/dist/eval/html-reporter.d.ts.map +1 -0
- package/dist/eval/html-reporter.js +209 -0
- package/dist/eval/html-reporter.js.map +1 -0
- package/dist/eval/index.d.ts +271 -0
- package/dist/eval/index.d.ts.map +1 -0
- package/dist/eval/index.js +510 -0
- package/dist/eval/index.js.map +1 -0
- package/dist/eval/json-reporter.d.ts +43 -0
- package/dist/eval/json-reporter.d.ts.map +1 -0
- package/dist/eval/json-reporter.js +40 -0
- package/dist/eval/json-reporter.js.map +1 -0
- package/dist/fake.d.ts +36 -1
- package/dist/fake.d.ts.map +1 -1
- package/dist/fake.js +49 -2
- package/dist/fake.js.map +1 -1
- package/dist/file-search.d.ts +168 -0
- package/dist/file-search.d.ts.map +1 -0
- package/dist/file-search.js +158 -0
- package/dist/file-search.js.map +1 -0
- package/dist/index.d.ts +22 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -1
- package/dist/index.js.map +1 -1
- package/dist/mcp/client-tools.d.ts +39 -0
- package/dist/mcp/client-tools.d.ts.map +1 -0
- package/dist/mcp/client-tools.js +147 -0
- package/dist/mcp/client-tools.js.map +1 -0
- package/dist/mcp/index.d.ts +16 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +15 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/server-from-agent.d.ts +24 -0
- package/dist/mcp/server-from-agent.d.ts.map +1 -0
- package/dist/mcp/server-from-agent.js +113 -0
- package/dist/mcp/server-from-agent.js.map +1 -0
- package/dist/mcp/types.d.ts +64 -0
- package/dist/mcp/types.d.ts.map +1 -0
- package/dist/mcp/types.js +6 -0
- package/dist/mcp/types.js.map +1 -0
- package/dist/memory-embedding/index.d.ts +121 -0
- package/dist/memory-embedding/index.d.ts.map +1 -0
- package/dist/memory-embedding/index.js +229 -0
- package/dist/memory-embedding/index.js.map +1 -0
- package/dist/memory-extract.d.ts +60 -0
- package/dist/memory-extract.d.ts.map +1 -0
- package/dist/memory-extract.js +163 -0
- package/dist/memory-extract.js.map +1 -0
- package/dist/memory-inject.d.ts +39 -0
- package/dist/memory-inject.d.ts.map +1 -0
- package/dist/memory-inject.js +135 -0
- package/dist/memory-inject.js.map +1 -0
- package/dist/memory-orm/index.d.ts +118 -0
- package/dist/memory-orm/index.d.ts.map +1 -0
- package/dist/memory-orm/index.js +187 -0
- package/dist/memory-orm/index.js.map +1 -0
- package/dist/memory.d.ts +55 -0
- package/dist/memory.d.ts.map +1 -0
- package/dist/memory.js +132 -0
- package/dist/memory.js.map +1 -0
- package/dist/observers.d.ts +22 -0
- package/dist/observers.d.ts.map +1 -1
- package/dist/observers.js.map +1 -1
- package/dist/provider-tools.d.ts +15 -1
- package/dist/provider-tools.d.ts.map +1 -1
- package/dist/provider-tools.js +21 -1
- package/dist/provider-tools.js.map +1 -1
- package/dist/providers/anthropic.d.ts.map +1 -1
- package/dist/providers/anthropic.js +61 -6
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/elevenlabs.d.ts +98 -0
- package/dist/providers/elevenlabs.d.ts.map +1 -0
- package/dist/providers/elevenlabs.js +229 -0
- package/dist/providers/elevenlabs.js.map +1 -0
- package/dist/providers/google.d.ts +83 -1
- package/dist/providers/google.d.ts.map +1 -1
- package/dist/providers/google.js +491 -8
- package/dist/providers/google.js.map +1 -1
- package/dist/providers/openai.d.ts +3 -1
- package/dist/providers/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +209 -5
- package/dist/providers/openai.js.map +1 -1
- package/dist/providers/voyage.d.ts +91 -0
- package/dist/providers/voyage.d.ts.map +1 -0
- package/dist/providers/voyage.js +166 -0
- package/dist/providers/voyage.js.map +1 -0
- package/dist/queue-job.d.ts +69 -4
- package/dist/queue-job.d.ts.map +1 -1
- package/dist/queue-job.js +114 -11
- package/dist/queue-job.js.map +1 -1
- package/dist/registry.d.ts +3 -1
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +10 -0
- package/dist/registry.js.map +1 -1
- package/dist/server/provider.d.ts.map +1 -1
- package/dist/server/provider.js +23 -1
- package/dist/server/provider.js.map +1 -1
- package/dist/similarity-search.d.ts +163 -0
- package/dist/similarity-search.d.ts.map +1 -0
- package/dist/similarity-search.js +147 -0
- package/dist/similarity-search.js.map +1 -0
- package/dist/tool.d.ts.map +1 -1
- package/dist/tool.js +13 -4
- package/dist/tool.js.map +1 -1
- package/dist/types.d.ts +246 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/vector-stores/index.d.ts +96 -0
- package/dist/vector-stores/index.d.ts.map +1 -0
- package/dist/vector-stores/index.js +153 -0
- package/dist/vector-stores/index.js.map +1 -0
- package/package.json +41 -3
package/dist/index.js
CHANGED
|
@@ -15,6 +15,8 @@ export { MistralProvider } from './providers/mistral.js';
|
|
|
15
15
|
export { AzureOpenAIProvider } from './providers/azure.js';
|
|
16
16
|
export { CohereProvider } from './providers/cohere.js';
|
|
17
17
|
export { JinaProvider } from './providers/jina.js';
|
|
18
|
+
export { ElevenLabsProvider, DEFAULT_TTS_MODEL_ID, DEFAULT_VOICE_ID } from './providers/elevenlabs.js';
|
|
19
|
+
export { VoyageProvider } from './providers/voyage.js';
|
|
18
20
|
export { OpenRouterProvider } from './providers/openrouter.js';
|
|
19
21
|
export { BedrockProvider } from './providers/bedrock.js';
|
|
20
22
|
// Tools
|
|
@@ -23,7 +25,7 @@ export { zodToJsonSchema } from './zod-to-json-schema.js';
|
|
|
23
25
|
// Handoffs
|
|
24
26
|
export { handoff, isHandoffTool } from './handoff.js';
|
|
25
27
|
// Agent
|
|
26
|
-
export { Agent, ConversableAgent, agent, stepCountIs, hasToolCall, setConversationStore } from './agent.js';
|
|
28
|
+
export { Agent, ConversableAgent, agent, stepCountIs, hasToolCall, setConversationStore, setUserMemory, resolveUserMemory } from './agent.js';
|
|
27
29
|
export { QueuedPromptBuilder } from './queue-job.js';
|
|
28
30
|
// Middleware
|
|
29
31
|
export { runOnConfig, runOnChunk, runOnBeforeToolCall, runOnAfterToolCall, runSequential, runOnUsage, runOnAbort, runOnError } from './middleware.js';
|
|
@@ -31,6 +33,10 @@ export { runOnConfig, runOnChunk, runOnBeforeToolCall, runOnAfterToolCall, runSe
|
|
|
31
33
|
export { Output } from './output.js';
|
|
32
34
|
// Conversation
|
|
33
35
|
export { MemoryConversationStore } from './conversation.js';
|
|
36
|
+
// User Memory (#A4)
|
|
37
|
+
export { MemoryUserMemory, resolveRemembersSpec } from './memory.js';
|
|
38
|
+
export { withMemoryInject } from './memory-inject.js';
|
|
39
|
+
export { withMemoryExtract } from './memory-extract.js';
|
|
34
40
|
// Sub-agent run store (asTool streaming + suspend)
|
|
35
41
|
export { InMemorySubAgentRunStore, CachedSubAgentRunStore, } from './sub-agent-run-store.js';
|
|
36
42
|
// Facade
|
|
@@ -48,8 +54,18 @@ export { toVercelDataStream, toVercelResponse } from './vercel-protocol.js';
|
|
|
48
54
|
export { Reranker } from './rerank.js';
|
|
49
55
|
// File Management
|
|
50
56
|
export { FileManager } from './files.js';
|
|
57
|
+
// Hosted Vector Stores (#B8 Phase 1)
|
|
58
|
+
export { VectorStores, VectorStore } from './vector-stores/index.js';
|
|
59
|
+
// File Search agent tool (#B8 Phase 2 — provider-native RAG)
|
|
60
|
+
export { fileSearch, isFileSearchTool, FILE_SEARCH_MARKER, FILE_SEARCH_TOOL_NAME, normalizeWhere } from './file-search.js';
|
|
51
61
|
// Cached Embeddings
|
|
52
62
|
export { CachedEmbeddingAdapter } from './cached-embedding.js';
|
|
63
|
+
// Similarity Search (#B7 Phase 2 — agent tool wrapping ORM vector primitives)
|
|
64
|
+
export { similaritySearch } from './similarity-search.js';
|
|
65
|
+
// Budget / pricing (#A6 — full pricing catalog + per-user spend caps)
|
|
66
|
+
export { ModelPricing, estimateCost, assertKnownModelPricing, UnknownModelPricingError, BudgetExceededError, } from './budget/pricing.js';
|
|
67
|
+
export { memoryBudgetStorage, periodKey, } from './budget/storage.js';
|
|
68
|
+
export { withBudget } from './budget/with-budget.js';
|
|
53
69
|
// Testing
|
|
54
70
|
export { AiFake } from './fake.js';
|
|
55
71
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAE3B,cAAc;AACd,OAAO,EAAE,kBAAkB,IAAI,QAAQ,EAAE,eAAe,IAAI,KAAK,EAAE,cAAc,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAE3B,cAAc;AACd,OAAO,EAAE,kBAAkB,IAAI,QAAQ,EAAE,eAAe,IAAI,KAAK,EAAE,cAAc,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAA;AAyErI,WAAW;AACX,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAE1C,YAAY;AACZ,OAAO,EAAE,iBAAiB,EAAwB,MAAM,0BAA0B,CAAA;AAClF,OAAO,EAAE,cAAc,EAAE,aAAa,EAAqB,MAAM,uBAAuB,CAAA;AACxF,OAAO,EAAE,cAAc,EAAqB,MAAM,uBAAuB,CAAA;AACzE,OAAO,EAAE,cAAc,EAAqB,MAAM,uBAAuB,CAAA;AACzE,OAAO,EAAE,gBAAgB,EAAuB,MAAM,yBAAyB,CAAA;AAC/E,OAAO,EAAE,WAAW,EAAkB,MAAM,oBAAoB,CAAA;AAChE,OAAO,EAAE,YAAY,EAAmB,MAAM,qBAAqB,CAAA;AACnE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AAqB,CAAA;AAE5B,OAAO,EACL,mBAAmB,EACnB,SAAS,GACV,MAAM,qBAAqB,CAAA;AAO5B,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AAOpD,UAAU;AACV,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Tool } from '../types.js';
|
|
2
|
+
import type { McpClientTransport, McpClientToolsOptions } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* The result of `mcpClientTools()` — an array of `Tool`s that also carries a
|
|
5
|
+
* `close()` method when this call owns the underlying client lifecycle.
|
|
6
|
+
*
|
|
7
|
+
* Spreading this into `tools()` works because the extra method is non-enumerable
|
|
8
|
+
* (and only present when relevant) — the agent loop iterates with for-of which
|
|
9
|
+
* skips it.
|
|
10
|
+
*/
|
|
11
|
+
export interface McpClientToolsHandle extends ReadonlyArray<Tool> {
|
|
12
|
+
/** Disconnect the underlying MCP client. No-op when an external client was passed in. */
|
|
13
|
+
close?: () => Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Connect to a remote MCP server and surface its tools as RudderJS `Tool`s.
|
|
17
|
+
*
|
|
18
|
+
* Three transport shapes are accepted:
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* // (a) HTTP — string URL or URL instance
|
|
22
|
+
* const t = await mcpClientTools('https://api.example.com/mcp')
|
|
23
|
+
*
|
|
24
|
+
* // (b) Local stdio subprocess
|
|
25
|
+
* const t = await mcpClientTools({ command: 'npx', args: ['some-mcp-server'] })
|
|
26
|
+
*
|
|
27
|
+
* // (c) Already-connected SDK Client (caller owns lifecycle)
|
|
28
|
+
* const t = await mcpClientTools(myClient)
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* The returned array exposes a `close()` method when this call owns the client
|
|
32
|
+
* (cases a + b). Pass it back so the subprocess / HTTP session can shut down
|
|
33
|
+
* cleanly when your agent is done.
|
|
34
|
+
*
|
|
35
|
+
* The remote server's `inputSchema` (JSON Schema) ships through to providers
|
|
36
|
+
* via `ToolDefinitionOptions.jsonSchema` — no zod conversion in either direction.
|
|
37
|
+
*/
|
|
38
|
+
export declare function mcpClientTools(transport: McpClientTransport, opts?: McpClientToolsOptions): Promise<McpClientToolsHandle>;
|
|
39
|
+
//# sourceMappingURL=client-tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-tools.d.ts","sourceRoot":"","sources":["../../src/mcp/client-tools.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAmB,MAAM,aAAa,CAAA;AACxD,OAAO,KAAK,EACV,kBAAkB,EAAE,qBAAqB,EAC1C,MAAM,YAAY,CAAA;AAInB;;;;;;;GAOG;AACH,MAAM,WAAW,oBAAqB,SAAQ,aAAa,CAAC,IAAI,CAAC;IAC/D,yFAAyF;IACzF,KAAK,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAC5B;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAsB,cAAc,CAClC,SAAS,EAAE,kBAAkB,EAC7B,IAAI,GAAE,qBAA0B,GAC/B,OAAO,CAAC,oBAAoB,CAAC,CA4B/B"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { dynamicTool } from '../tool.js';
|
|
3
|
+
const CLIENT_INFO = { name: 'rudderjs-ai-mcp-bridge', version: '1.0.0' };
|
|
4
|
+
/**
|
|
5
|
+
* Connect to a remote MCP server and surface its tools as RudderJS `Tool`s.
|
|
6
|
+
*
|
|
7
|
+
* Three transport shapes are accepted:
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* // (a) HTTP — string URL or URL instance
|
|
11
|
+
* const t = await mcpClientTools('https://api.example.com/mcp')
|
|
12
|
+
*
|
|
13
|
+
* // (b) Local stdio subprocess
|
|
14
|
+
* const t = await mcpClientTools({ command: 'npx', args: ['some-mcp-server'] })
|
|
15
|
+
*
|
|
16
|
+
* // (c) Already-connected SDK Client (caller owns lifecycle)
|
|
17
|
+
* const t = await mcpClientTools(myClient)
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* The returned array exposes a `close()` method when this call owns the client
|
|
21
|
+
* (cases a + b). Pass it back so the subprocess / HTTP session can shut down
|
|
22
|
+
* cleanly when your agent is done.
|
|
23
|
+
*
|
|
24
|
+
* The remote server's `inputSchema` (JSON Schema) ships through to providers
|
|
25
|
+
* via `ToolDefinitionOptions.jsonSchema` — no zod conversion in either direction.
|
|
26
|
+
*/
|
|
27
|
+
export async function mcpClientTools(transport, opts = {}) {
|
|
28
|
+
const streaming = opts.streaming ?? true;
|
|
29
|
+
const namePrefix = opts.namePrefix ?? '';
|
|
30
|
+
const { client, ownsClient } = await resolveClient(transport);
|
|
31
|
+
let toolList;
|
|
32
|
+
try {
|
|
33
|
+
const listed = await client.listTools();
|
|
34
|
+
toolList = listed.tools.filter(t => opts.filter ? opts.filter(t.name) : true);
|
|
35
|
+
}
|
|
36
|
+
catch (err) {
|
|
37
|
+
if (ownsClient)
|
|
38
|
+
await safeClose(client);
|
|
39
|
+
throw err;
|
|
40
|
+
}
|
|
41
|
+
const tools = toolList.map(t => buildTool(client, t, namePrefix, streaming));
|
|
42
|
+
const handle = ownsClient
|
|
43
|
+
? Object.defineProperty([...tools], 'close', {
|
|
44
|
+
value: () => safeClose(client),
|
|
45
|
+
enumerable: false,
|
|
46
|
+
writable: false,
|
|
47
|
+
})
|
|
48
|
+
: tools;
|
|
49
|
+
return handle;
|
|
50
|
+
}
|
|
51
|
+
async function resolveClient(transport) {
|
|
52
|
+
// Already a Client instance — duck-type check for `callTool` + `listTools`.
|
|
53
|
+
if (typeof transport === 'object' && transport !== null && 'callTool' in transport && 'listTools' in transport) {
|
|
54
|
+
return { client: transport, ownsClient: false };
|
|
55
|
+
}
|
|
56
|
+
// Lazy-load the SDK so apps that don't import @rudderjs/ai/mcp don't pay for it.
|
|
57
|
+
const { Client } = await import('@modelcontextprotocol/sdk/client/index.js');
|
|
58
|
+
const sdkTransport = await buildTransport(transport);
|
|
59
|
+
const client = new Client(CLIENT_INFO);
|
|
60
|
+
await client.connect(sdkTransport);
|
|
61
|
+
return { client, ownsClient: true };
|
|
62
|
+
}
|
|
63
|
+
async function buildTransport(transport) {
|
|
64
|
+
if (typeof transport === 'string' || transport instanceof URL) {
|
|
65
|
+
const url = transport instanceof URL ? transport : new URL(transport);
|
|
66
|
+
const { StreamableHTTPClientTransport } = await import('@modelcontextprotocol/sdk/client/streamableHttp.js');
|
|
67
|
+
return new StreamableHTTPClientTransport(url);
|
|
68
|
+
}
|
|
69
|
+
if (typeof transport === 'object' && transport !== null && 'command' in transport) {
|
|
70
|
+
const spawn = transport;
|
|
71
|
+
const { StdioClientTransport } = await import('@modelcontextprotocol/sdk/client/stdio.js');
|
|
72
|
+
return new StdioClientTransport({
|
|
73
|
+
command: spawn.command,
|
|
74
|
+
...(spawn.args !== undefined ? { args: [...spawn.args] } : {}),
|
|
75
|
+
...(spawn.env !== undefined ? { env: spawn.env } : {}),
|
|
76
|
+
...(spawn.cwd !== undefined ? { cwd: spawn.cwd } : {}),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
throw new Error(`mcpClientTools: unsupported transport shape: ${typeof transport}`);
|
|
80
|
+
}
|
|
81
|
+
async function safeClose(client) {
|
|
82
|
+
try {
|
|
83
|
+
await client.close();
|
|
84
|
+
}
|
|
85
|
+
catch { /* best-effort */ }
|
|
86
|
+
}
|
|
87
|
+
function buildTool(client, remote, namePrefix, streaming) {
|
|
88
|
+
const localName = namePrefix + remote.name;
|
|
89
|
+
const builder = dynamicTool({
|
|
90
|
+
name: localName,
|
|
91
|
+
description: remote.description ?? '',
|
|
92
|
+
inputSchema: z.unknown(), // placeholder — real shape lives in jsonSchema
|
|
93
|
+
jsonSchema: remote.inputSchema,
|
|
94
|
+
});
|
|
95
|
+
if (streaming) {
|
|
96
|
+
const built = builder.server(async function* (input, _ctx) {
|
|
97
|
+
const collected = [];
|
|
98
|
+
const result = await client.callTool({ name: remote.name, arguments: (input ?? {}) }, undefined, { onprogress: (p) => collected.push(p) });
|
|
99
|
+
// SDK delivers progress notifications synchronously into onprogress during
|
|
100
|
+
// the request lifetime, so by the time we're here all progress events have
|
|
101
|
+
// arrived. Yielding them before returning preserves the observable order
|
|
102
|
+
// (tool-update chunks land before tool-result).
|
|
103
|
+
for (const p of collected)
|
|
104
|
+
yield p;
|
|
105
|
+
return mcpContentToString(result);
|
|
106
|
+
});
|
|
107
|
+
return built;
|
|
108
|
+
}
|
|
109
|
+
const built = builder.server(async (input) => {
|
|
110
|
+
const result = await client.callTool({ name: remote.name, arguments: (input ?? {}) });
|
|
111
|
+
return mcpContentToString(result);
|
|
112
|
+
});
|
|
113
|
+
return built;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Flatten an MCP tool result into a string for the agent's `tool_result` slot.
|
|
117
|
+
* Text blocks concatenate; image / resource blocks become bracketed placeholders
|
|
118
|
+
* so the model knows something non-text was returned.
|
|
119
|
+
*/
|
|
120
|
+
function mcpContentToString(result) {
|
|
121
|
+
const parts = [];
|
|
122
|
+
for (const block of result.content) {
|
|
123
|
+
if (typeof block !== 'object' || block === null)
|
|
124
|
+
continue;
|
|
125
|
+
const b = block;
|
|
126
|
+
if (b['type'] === 'text' && typeof b['text'] === 'string') {
|
|
127
|
+
parts.push(b['text']);
|
|
128
|
+
}
|
|
129
|
+
else if (b['type'] === 'image') {
|
|
130
|
+
parts.push(`[image: ${b['mimeType'] ?? 'unknown mime'}]`);
|
|
131
|
+
}
|
|
132
|
+
else if (b['type'] === 'resource' || b['type'] === 'resource_link') {
|
|
133
|
+
const ref = b['resource'] && typeof b['resource'] === 'object'
|
|
134
|
+
? b['resource']['uri']
|
|
135
|
+
: b['uri'];
|
|
136
|
+
parts.push(`[resource: ${ref ?? 'unknown'}]`);
|
|
137
|
+
}
|
|
138
|
+
else if (b['type']) {
|
|
139
|
+
parts.push(`[${b['type']}]`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const text = parts.join('\n').trim();
|
|
143
|
+
if (result.isError)
|
|
144
|
+
return `[error] ${text || 'Tool reported an error'}`;
|
|
145
|
+
return text || '(empty result)';
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=client-tools.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-tools.js","sourceRoot":"","sources":["../../src/mcp/client-tools.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAMxC,MAAM,WAAW,GAAG,EAAE,IAAI,EAAE,wBAAwB,EAAE,OAAO,EAAE,OAAO,EAAW,CAAA;AAejF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,SAA6B,EAC7B,OAA8B,EAAE;IAEhC,MAAM,SAAS,GAAI,IAAI,CAAC,SAAS,IAAI,IAAI,CAAA;IACzC,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,IAAI,EAAE,CAAA;IAExC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,MAAM,aAAa,CAAC,SAAS,CAAC,CAAA;IAE7D,IAAI,QAA2B,CAAA;IAC/B,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,SAAS,EAAE,CAAA;QACvC,QAAQ,GAAI,MAAM,CAAC,KAAsB,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CACnD,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CACzC,CAAA;IACH,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,UAAU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IAAI,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QAC9B,CAAC;IACH,CAAC;IACD,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,CAAA;IACpC,IAAI,MAAM,CAAC,OAAO;QAAE,OAAO,WAAW,IAAI,IAAI,wBAAwB,EAAE,CAAA;IACxE,OAAO,IAAI,IAAI,gBAAgB,CAAA;AACjC,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@rudderjs/ai/mcp` — bridge between `@rudderjs/ai` Agents and Model Context
|
|
3
|
+
* Protocol servers. Two connectors:
|
|
4
|
+
*
|
|
5
|
+
* - {@link mcpClientTools} — consume a remote MCP server's tools as Agent tools
|
|
6
|
+
* - {@link mcpServerFromAgent} — expose an Agent as an MCP server external
|
|
7
|
+
* clients (Claude Desktop, Cursor, etc.) can call
|
|
8
|
+
*
|
|
9
|
+
* Requires `@modelcontextprotocol/sdk` at runtime — declared as an optional
|
|
10
|
+
* peer dependency. Apps that don't import this subpath aren't forced to
|
|
11
|
+
* install it.
|
|
12
|
+
*/
|
|
13
|
+
export { mcpClientTools } from './client-tools.js';
|
|
14
|
+
export { mcpServerFromAgent } from './server-from-agent.js';
|
|
15
|
+
export type { McpClientTransport, McpClientToolsOptions, McpServerFromAgentOptions, StdioServerSpawn, } from './types.js';
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/mcp/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAClD,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAC3D,YAAY,EACV,kBAAkB,EAClB,qBAAqB,EACrB,yBAAyB,EACzB,gBAAgB,GACjB,MAAM,YAAY,CAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@rudderjs/ai/mcp` — bridge between `@rudderjs/ai` Agents and Model Context
|
|
3
|
+
* Protocol servers. Two connectors:
|
|
4
|
+
*
|
|
5
|
+
* - {@link mcpClientTools} — consume a remote MCP server's tools as Agent tools
|
|
6
|
+
* - {@link mcpServerFromAgent} — expose an Agent as an MCP server external
|
|
7
|
+
* clients (Claude Desktop, Cursor, etc.) can call
|
|
8
|
+
*
|
|
9
|
+
* Requires `@modelcontextprotocol/sdk` at runtime — declared as an optional
|
|
10
|
+
* peer dependency. Apps that don't import this subpath aren't forced to
|
|
11
|
+
* install it.
|
|
12
|
+
*/
|
|
13
|
+
export { mcpClientTools } from './client-tools.js';
|
|
14
|
+
export { mcpServerFromAgent } from './server-from-agent.js';
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/mcp/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAClD,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Agent } from '../agent.js';
|
|
2
|
+
import type { McpServerFromAgentOptions } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Wrap an `Agent` class as an MCP server. External MCP clients (Claude Desktop,
|
|
5
|
+
* Cursor, etc.) can connect to it like any other MCP server.
|
|
6
|
+
*
|
|
7
|
+
* Returns an `McpServer` from `@modelcontextprotocol/sdk` — connect it with the
|
|
8
|
+
* SDK's stdio / HTTP transports:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { mcpServerFromAgent } from '@rudderjs/ai/mcp'
|
|
12
|
+
* import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
|
|
13
|
+
*
|
|
14
|
+
* const server = await mcpServerFromAgent(MyAgent)
|
|
15
|
+
* await server.connect(new StdioServerTransport())
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* Three exposure modes via `opts.expose`:
|
|
19
|
+
* - `'tools'` (default) — one MCP tool per `agent.tools()` entry
|
|
20
|
+
* - `'agent'` — one MCP tool that runs the whole agent (`prompt(text) → text`)
|
|
21
|
+
* - `'both'` — expose individual tools and the agent prompt-tool side by side
|
|
22
|
+
*/
|
|
23
|
+
export declare function mcpServerFromAgent(AgentClass: new () => Agent, opts?: McpServerFromAgentOptions): Promise<unknown>;
|
|
24
|
+
//# sourceMappingURL=server-from-agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-from-agent.d.ts","sourceRoot":"","sources":["../../src/mcp/server-from-agent.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AAExC,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAA;AAE3D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,kBAAkB,CACtC,UAAU,EAAE,UAAU,KAAK,EAC3B,IAAI,GAAE,yBAA8B,GACnC,OAAO,CAAC,OAAO,CAAC,CAiClB"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Wrap an `Agent` class as an MCP server. External MCP clients (Claude Desktop,
|
|
4
|
+
* Cursor, etc.) can connect to it like any other MCP server.
|
|
5
|
+
*
|
|
6
|
+
* Returns an `McpServer` from `@modelcontextprotocol/sdk` — connect it with the
|
|
7
|
+
* SDK's stdio / HTTP transports:
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { mcpServerFromAgent } from '@rudderjs/ai/mcp'
|
|
11
|
+
* import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
|
|
12
|
+
*
|
|
13
|
+
* const server = await mcpServerFromAgent(MyAgent)
|
|
14
|
+
* await server.connect(new StdioServerTransport())
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Three exposure modes via `opts.expose`:
|
|
18
|
+
* - `'tools'` (default) — one MCP tool per `agent.tools()` entry
|
|
19
|
+
* - `'agent'` — one MCP tool that runs the whole agent (`prompt(text) → text`)
|
|
20
|
+
* - `'both'` — expose individual tools and the agent prompt-tool side by side
|
|
21
|
+
*/
|
|
22
|
+
export async function mcpServerFromAgent(AgentClass, opts = {}) {
|
|
23
|
+
const expose = opts.expose ?? 'tools';
|
|
24
|
+
const { McpServer } = await import('@modelcontextprotocol/sdk/server/mcp.js');
|
|
25
|
+
const agent = new AgentClass();
|
|
26
|
+
const instructions = opts.instructions ?? safeInstructions(agent);
|
|
27
|
+
const name = opts.name ?? `${AgentClass.name}Server`;
|
|
28
|
+
const version = opts.version ?? '1.0.0';
|
|
29
|
+
const server = new McpServer({ name, version }, instructions !== undefined ? { instructions } : {});
|
|
30
|
+
if (expose === 'tools' || expose === 'both') {
|
|
31
|
+
// `tools()` is opt-in via the HasTools interface — abstract `Agent` doesn't
|
|
32
|
+
// declare it. Cast and tolerate absence gracefully (subclass without tools()
|
|
33
|
+
// returns no tools but the agent prompt-tool path still works).
|
|
34
|
+
const hasTools = agent;
|
|
35
|
+
const toolList = typeof hasTools.tools === 'function' ? hasTools.tools() : [];
|
|
36
|
+
for (const tool of toolList) {
|
|
37
|
+
registerAgentToolOnServer(server, tool);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (expose === 'agent' || expose === 'both') {
|
|
41
|
+
registerAgentPromptToolOnServer(server, AgentClass, opts.agentToolName ?? AgentClass.name);
|
|
42
|
+
}
|
|
43
|
+
return server;
|
|
44
|
+
}
|
|
45
|
+
function registerAgentToolOnServer(server, tool) {
|
|
46
|
+
const name = tool.definition.name;
|
|
47
|
+
const description = tool.definition.description;
|
|
48
|
+
// Pass the zod schema as a single AnySchema (the SDK's `inputSchema` field
|
|
49
|
+
// accepts either a ZodRawShape record or a single zod schema). The callback
|
|
50
|
+
// then receives `input: unknown` which is what the agent tool expects too.
|
|
51
|
+
server.registerTool(name, { description, inputSchema: tool.definition.inputSchema }, async (input) => {
|
|
52
|
+
const result = await runAgentTool(tool, input);
|
|
53
|
+
return { content: [{ type: 'text', text: stringifyResult(result) }] };
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function registerAgentPromptToolOnServer(server, AgentClass, toolName) {
|
|
57
|
+
const promptShape = { prompt: z.string().describe('User message for the agent') };
|
|
58
|
+
server.registerTool(toolName, {
|
|
59
|
+
description: `Run the ${AgentClass.name} agent with a prompt and return its response`,
|
|
60
|
+
inputSchema: promptShape,
|
|
61
|
+
}, async (input) => {
|
|
62
|
+
const args = (input ?? {});
|
|
63
|
+
if (typeof args.prompt !== 'string') {
|
|
64
|
+
return { content: [{ type: 'text', text: '[error] Agent prompt-tool requires { prompt: string }' }] };
|
|
65
|
+
}
|
|
66
|
+
const agent = new AgentClass();
|
|
67
|
+
const response = await agent.prompt(args.prompt);
|
|
68
|
+
return { content: [{ type: 'text', text: response.text ?? '' }] };
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
async function runAgentTool(tool, input) {
|
|
72
|
+
if (!tool.execute) {
|
|
73
|
+
throw new Error(`mcpServerFromAgent: tool "${tool.definition.name}" has no execute fn (client-only tool — cannot be exposed via MCP)`);
|
|
74
|
+
}
|
|
75
|
+
const out = tool.execute(input);
|
|
76
|
+
if (out instanceof Promise)
|
|
77
|
+
return await out;
|
|
78
|
+
// Generator path — drain progress yields silently and return the final value.
|
|
79
|
+
// (MCP forwards progress via `notifications/progress`; the agent loop's
|
|
80
|
+
// tool-update chunks don't map cleanly without a progressToken from the
|
|
81
|
+
// caller, so we drop them in v1. Future enhancement: forward progress when
|
|
82
|
+
// the calling MCP client supplied a progressToken.)
|
|
83
|
+
const iter = out;
|
|
84
|
+
let next = await iter.next();
|
|
85
|
+
while (!next.done)
|
|
86
|
+
next = await iter.next();
|
|
87
|
+
return next.value;
|
|
88
|
+
}
|
|
89
|
+
function stringifyResult(result) {
|
|
90
|
+
if (typeof result === 'string')
|
|
91
|
+
return result;
|
|
92
|
+
if (result === undefined || result === null)
|
|
93
|
+
return '';
|
|
94
|
+
if (typeof result === 'object') {
|
|
95
|
+
try {
|
|
96
|
+
return JSON.stringify(result, null, 2);
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
return String(result);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return String(result);
|
|
103
|
+
}
|
|
104
|
+
function safeInstructions(agent) {
|
|
105
|
+
try {
|
|
106
|
+
const out = agent.instructions();
|
|
107
|
+
return typeof out === 'string' && out.length > 0 ? out : undefined;
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=server-from-agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-from-agent.js","sourceRoot":"","sources":["../../src/mcp/server-from-agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAKvB;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,UAA2B,EAC3B,OAAkC,EAAE;IAEpC,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,IAAI,OAAO,CAAA;IAErC,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,MAAM,CAAC,yCAAyC,CAE3E,CAAA;IAED,MAAM,KAAK,GAAG,IAAI,UAAU,EAAE,CAAA;IAC9B,MAAM,YAAY,GAAG,IAAI,CAAC,YAAY,IAAI,gBAAgB,CAAC,KAAK,CAAC,CAAA;IACjE,MAAM,IAAI,GAAM,IAAI,CAAC,IAAI,IAAO,GAAG,UAAU,CAAC,IAAI,QAAQ,CAAA;IAC1D,MAA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,CAAA;IAAC,CAAC;AAC9B,CAAC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public types for `@rudderjs/ai/mcp`. Kept in a separate module so the
|
|
3
|
+
* client + server connectors can share them without circular imports.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Configuration for spawning a local MCP server as a stdio subprocess.
|
|
7
|
+
* Mirrors `StdioServerParameters` from `@modelcontextprotocol/sdk` but is
|
|
8
|
+
* re-exported here so consumers don't need a direct SDK import for the
|
|
9
|
+
* common case.
|
|
10
|
+
*/
|
|
11
|
+
export interface StdioServerSpawn {
|
|
12
|
+
command: string;
|
|
13
|
+
args?: readonly string[];
|
|
14
|
+
/** Inherited from the parent process when omitted. */
|
|
15
|
+
env?: Readonly<Record<string, string>>;
|
|
16
|
+
/** Working directory for the spawned process. */
|
|
17
|
+
cwd?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Anything `mcpClientTools()` accepts as the connection target.
|
|
21
|
+
*
|
|
22
|
+
* - `string` / `URL` — connects via the Streamable HTTP transport
|
|
23
|
+
* - `StdioServerSpawn` — spawns a subprocess and connects over stdio
|
|
24
|
+
* - existing `Client` instance — used as-is, lifecycle remains the caller's
|
|
25
|
+
*
|
|
26
|
+
* Implementation note: keeping `Client` as `unknown` here so the type union
|
|
27
|
+
* doesn't force a hard dep on `@modelcontextprotocol/sdk` at module load.
|
|
28
|
+
* The runtime code uses an instanceof check via dynamic import.
|
|
29
|
+
*/
|
|
30
|
+
export type McpClientTransport = string | URL | StdioServerSpawn | object;
|
|
31
|
+
export interface McpClientToolsOptions {
|
|
32
|
+
/** Filter exposed tools — return `false` to drop a remote tool from the result. Defaults to all. */
|
|
33
|
+
filter?: (toolName: string) => boolean;
|
|
34
|
+
/** Prefix tool names to avoid collisions when wiring multiple remote servers. */
|
|
35
|
+
namePrefix?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Forward MCP `notifications/progress` from the remote server as `tool-update`
|
|
38
|
+
* chunks during agent execution. Defaults to `true`.
|
|
39
|
+
*/
|
|
40
|
+
streaming?: boolean;
|
|
41
|
+
}
|
|
42
|
+
export interface McpServerFromAgentOptions {
|
|
43
|
+
/** Server name advertised over MCP. Default: `${AgentClass.name}Server`. */
|
|
44
|
+
name?: string;
|
|
45
|
+
/** Server version. Default: `'1.0.0'`. */
|
|
46
|
+
version?: string;
|
|
47
|
+
/**
|
|
48
|
+
* Server instructions advertised over MCP — typically the agent's own
|
|
49
|
+
* system prompt. Default: tries `agent.instructions()`, falls back to undefined.
|
|
50
|
+
*/
|
|
51
|
+
instructions?: string;
|
|
52
|
+
/**
|
|
53
|
+
* What to expose:
|
|
54
|
+
* - `'tools'` (default): one MCP tool per `agent.tools()` entry — external
|
|
55
|
+
* MCP clients call them as if they were the server's own.
|
|
56
|
+
* - `'agent'`: one MCP tool `prompt(text: string)` that runs the whole agent
|
|
57
|
+
* and returns the response text. Ship one agent, callable from any MCP client.
|
|
58
|
+
* - `'both'`: both of the above.
|
|
59
|
+
*/
|
|
60
|
+
expose?: 'tools' | 'agent' | 'both';
|
|
61
|
+
/** Name of the synthetic prompt-tool when `expose: 'agent' | 'both'`. Default: agent class name. */
|
|
62
|
+
agentToolName?: string;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/mcp/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;GAKG;AACH,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAI,SAAS,MAAM,EAAE,CAAA;IAC1B,sDAAsD;IACtD,GAAG,CAAC,EAAK,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;IACzC,iDAAiD;IACjD,GAAG,CAAC,EAAK,MAAM,CAAA;CAChB;AAED;;;;;;;;;;GAUG;AACH,MAAM,MAAM,kBAAkB,GAC1B,MAAM,GACN,GAAG,GACH,gBAAgB,GAChB,MAAM,CAAA;AAEV,MAAM,WAAW,qBAAqB;IACpC,oGAAoG;IACpG,MAAM,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAA;IACtC,iFAAiF;IACjF,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;CACpB;AAED,MAAM,WAAW,yBAAyB;IACxC,4EAA4E;IAC5E,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,0CAA0C;IAC1C,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB;;;;;;;OAOG;IACH,MAAM,CAAC,EAAE,OAAO,GAAG,OAAO,GAAG,MAAM,CAAA;IACnC,oGAAoG;IACpG,aAAa,CAAC,EAAE,MAAM,CAAA;CACvB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/mcp/types.ts"],"names":[],"mappings":"AAAA;;;GAGG"}
|