@mcp-ts/sdk 1.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/LICENSE +21 -0
- package/README.md +297 -0
- package/dist/adapters/agui-adapter.d.mts +119 -0
- package/dist/adapters/agui-adapter.d.ts +119 -0
- package/dist/adapters/agui-adapter.js +109 -0
- package/dist/adapters/agui-adapter.js.map +1 -0
- package/dist/adapters/agui-adapter.mjs +107 -0
- package/dist/adapters/agui-adapter.mjs.map +1 -0
- package/dist/adapters/agui-middleware.d.mts +171 -0
- package/dist/adapters/agui-middleware.d.ts +171 -0
- package/dist/adapters/agui-middleware.js +429 -0
- package/dist/adapters/agui-middleware.js.map +1 -0
- package/dist/adapters/agui-middleware.mjs +417 -0
- package/dist/adapters/agui-middleware.mjs.map +1 -0
- package/dist/adapters/ai-adapter.d.mts +38 -0
- package/dist/adapters/ai-adapter.d.ts +38 -0
- package/dist/adapters/ai-adapter.js +82 -0
- package/dist/adapters/ai-adapter.js.map +1 -0
- package/dist/adapters/ai-adapter.mjs +80 -0
- package/dist/adapters/ai-adapter.mjs.map +1 -0
- package/dist/adapters/langchain-adapter.d.mts +46 -0
- package/dist/adapters/langchain-adapter.d.ts +46 -0
- package/dist/adapters/langchain-adapter.js +102 -0
- package/dist/adapters/langchain-adapter.js.map +1 -0
- package/dist/adapters/langchain-adapter.mjs +100 -0
- package/dist/adapters/langchain-adapter.mjs.map +1 -0
- package/dist/adapters/mastra-adapter.d.mts +49 -0
- package/dist/adapters/mastra-adapter.d.ts +49 -0
- package/dist/adapters/mastra-adapter.js +95 -0
- package/dist/adapters/mastra-adapter.js.map +1 -0
- package/dist/adapters/mastra-adapter.mjs +93 -0
- package/dist/adapters/mastra-adapter.mjs.map +1 -0
- package/dist/client/index.d.mts +119 -0
- package/dist/client/index.d.ts +119 -0
- package/dist/client/index.js +225 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/index.mjs +223 -0
- package/dist/client/index.mjs.map +1 -0
- package/dist/client/react.d.mts +151 -0
- package/dist/client/react.d.ts +151 -0
- package/dist/client/react.js +492 -0
- package/dist/client/react.js.map +1 -0
- package/dist/client/react.mjs +489 -0
- package/dist/client/react.mjs.map +1 -0
- package/dist/client/vue.d.mts +157 -0
- package/dist/client/vue.d.ts +157 -0
- package/dist/client/vue.js +474 -0
- package/dist/client/vue.js.map +1 -0
- package/dist/client/vue.mjs +471 -0
- package/dist/client/vue.mjs.map +1 -0
- package/dist/events-BP6WyRNh.d.mts +110 -0
- package/dist/events-BP6WyRNh.d.ts +110 -0
- package/dist/index.d.mts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +2784 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2723 -0
- package/dist/index.mjs.map +1 -0
- package/dist/multi-session-client-BOFgPypS.d.ts +389 -0
- package/dist/multi-session-client-DMF3ED2O.d.mts +389 -0
- package/dist/server/index.d.mts +269 -0
- package/dist/server/index.d.ts +269 -0
- package/dist/server/index.js +2444 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/index.mjs +2414 -0
- package/dist/server/index.mjs.map +1 -0
- package/dist/shared/index.d.mts +24 -0
- package/dist/shared/index.d.ts +24 -0
- package/dist/shared/index.js +223 -0
- package/dist/shared/index.js.map +1 -0
- package/dist/shared/index.mjs +190 -0
- package/dist/shared/index.mjs.map +1 -0
- package/dist/types-SbDlA2VX.d.mts +153 -0
- package/dist/types-SbDlA2VX.d.ts +153 -0
- package/dist/utils-0qmYrqoa.d.mts +92 -0
- package/dist/utils-0qmYrqoa.d.ts +92 -0
- package/package.json +165 -0
- package/src/adapters/agui-adapter.ts +210 -0
- package/src/adapters/agui-middleware.ts +512 -0
- package/src/adapters/ai-adapter.ts +115 -0
- package/src/adapters/langchain-adapter.ts +127 -0
- package/src/adapters/mastra-adapter.ts +126 -0
- package/src/client/core/sse-client.ts +340 -0
- package/src/client/index.ts +26 -0
- package/src/client/react/index.ts +10 -0
- package/src/client/react/useMcp.ts +558 -0
- package/src/client/vue/index.ts +10 -0
- package/src/client/vue/useMcp.ts +542 -0
- package/src/index.ts +11 -0
- package/src/server/handlers/nextjs-handler.ts +216 -0
- package/src/server/handlers/sse-handler.ts +699 -0
- package/src/server/index.ts +57 -0
- package/src/server/mcp/multi-session-client.ts +132 -0
- package/src/server/mcp/oauth-client.ts +1168 -0
- package/src/server/mcp/storage-oauth-provider.ts +239 -0
- package/src/server/storage/file-backend.ts +169 -0
- package/src/server/storage/index.ts +115 -0
- package/src/server/storage/memory-backend.ts +132 -0
- package/src/server/storage/redis-backend.ts +210 -0
- package/src/server/storage/redis.ts +160 -0
- package/src/server/storage/types.ts +109 -0
- package/src/shared/constants.ts +29 -0
- package/src/shared/errors.ts +133 -0
- package/src/shared/events.ts +166 -0
- package/src/shared/index.ts +70 -0
- package/src/shared/types.ts +274 -0
- package/src/shared/utils.ts +16 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Adapter for AG-UI Integration
|
|
3
|
+
*
|
|
4
|
+
* This adapter transforms MCP tools into formats compatible with AG-UI agents.
|
|
5
|
+
* It provides tools with handlers for server-side execution and tool definitions
|
|
6
|
+
* in JSON Schema format for passing to remote agents.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { MultiSessionClient } from '@mcp-ts/sdk/server';
|
|
11
|
+
* import { AguiAdapter } from '@mcp-ts/sdk/adapters/mcp-adapter';
|
|
12
|
+
* import { createMcpMiddleware } from '@mcp-ts/sdk/adapters/agui-middleware';
|
|
13
|
+
* import { HttpAgent } from '@ag-ui/client';
|
|
14
|
+
*
|
|
15
|
+
* // Create MCP client
|
|
16
|
+
* const mcpClient = new MultiSessionClient('user_123');
|
|
17
|
+
* await mcpClient.connect();
|
|
18
|
+
*
|
|
19
|
+
* // Create adapter and get tools
|
|
20
|
+
* const adapter = new AguiAdapter(mcpClient);
|
|
21
|
+
* const tools = await adapter.getTools();
|
|
22
|
+
*
|
|
23
|
+
* // Use with AG-UI middleware
|
|
24
|
+
* const agent = new HttpAgent({ url: 'http://localhost:8000/agent' });
|
|
25
|
+
* agent.use(createMcpMiddleware(mcpClient, { tools }));
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { MCPClient } from '../server/mcp/oauth-client.js';
|
|
30
|
+
import { MultiSessionClient } from '../server/mcp/multi-session-client.js';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Configuration options for AguiAdapter
|
|
34
|
+
*/
|
|
35
|
+
export interface AguiAdapterOptions {
|
|
36
|
+
/**
|
|
37
|
+
* Prefix for tool names to avoid collision with other tools.
|
|
38
|
+
* @default serverId or 'mcp'
|
|
39
|
+
*/
|
|
40
|
+
prefix?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* AG-UI Tool with handler for server-side execution.
|
|
45
|
+
*
|
|
46
|
+
* Tools contain:
|
|
47
|
+
* - `name`: Unique identifier (prefixed with server ID)
|
|
48
|
+
* - `description`: Human-readable description for the LLM
|
|
49
|
+
* - `parameters`: JSON Schema defining the input format
|
|
50
|
+
* - `handler`: Function that executes the tool via MCP client
|
|
51
|
+
*/
|
|
52
|
+
export interface AguiTool {
|
|
53
|
+
/** Unique tool name (e.g., "server-abc_get_weather") */
|
|
54
|
+
name: string;
|
|
55
|
+
/** Human-readable description for the LLM */
|
|
56
|
+
description: string;
|
|
57
|
+
/** JSON Schema format parameters */
|
|
58
|
+
parameters?: Record<string, any>;
|
|
59
|
+
/** Handler function that executes the MCP tool */
|
|
60
|
+
handler?: (args: any) => any | Promise<any>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Tool definition format for passing to remote agents (without handler).
|
|
65
|
+
* Compatible with OpenAI's function calling API.
|
|
66
|
+
*/
|
|
67
|
+
export interface AguiToolDefinition {
|
|
68
|
+
/** Tool name (e.g., "server-abc_get_weather") */
|
|
69
|
+
name: string;
|
|
70
|
+
/** Human-readable description */
|
|
71
|
+
description: string;
|
|
72
|
+
/** JSON Schema format parameters */
|
|
73
|
+
parameters: Record<string, any>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Adapter that transforms MCP tools into AG-UI compatible formats.
|
|
78
|
+
*
|
|
79
|
+
* This adapter provides two main outputs:
|
|
80
|
+
* - `getTools()`: Returns tools with handlers for server-side execution
|
|
81
|
+
* - `getToolDefinitions()`: Returns tool definitions in JSON Schema format for remote agents
|
|
82
|
+
*/
|
|
83
|
+
export class AguiAdapter {
|
|
84
|
+
constructor(
|
|
85
|
+
private client: MCPClient | MultiSessionClient,
|
|
86
|
+
private options: AguiAdapterOptions = {}
|
|
87
|
+
) { }
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Get tools with handlers for MCP tool execution.
|
|
91
|
+
*
|
|
92
|
+
* Each tool includes a handler function that:
|
|
93
|
+
* 1. Calls the MCP tool via the client
|
|
94
|
+
* 2. Extracts text content from the result
|
|
95
|
+
* 3. Returns the result as a string or JSON
|
|
96
|
+
*
|
|
97
|
+
* @returns Array of AguiTool objects
|
|
98
|
+
*/
|
|
99
|
+
async getTools(): Promise<AguiTool[]> {
|
|
100
|
+
const isMultiSession = typeof (this.client as any).getClients === 'function';
|
|
101
|
+
|
|
102
|
+
if (isMultiSession) {
|
|
103
|
+
const clients = (this.client as MultiSessionClient).getClients();
|
|
104
|
+
const allTools: AguiTool[] = [];
|
|
105
|
+
|
|
106
|
+
for (const client of clients) {
|
|
107
|
+
const tools = await this.transformTools(client);
|
|
108
|
+
allTools.push(...tools);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return allTools;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return this.transformTools(this.client as MCPClient);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private async transformTools(client: MCPClient): Promise<AguiTool[]> {
|
|
118
|
+
if (!client.isConnected()) {
|
|
119
|
+
return [];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const result = await client.listTools();
|
|
123
|
+
const prefix = this.options.prefix ?? client.getServerId() ?? 'mcp';
|
|
124
|
+
const tools: AguiTool[] = [];
|
|
125
|
+
|
|
126
|
+
for (const tool of result.tools) {
|
|
127
|
+
const toolName = `${prefix}_${tool.name}`;
|
|
128
|
+
|
|
129
|
+
tools.push({
|
|
130
|
+
name: toolName,
|
|
131
|
+
description: tool.description || `Execute ${tool.name}`,
|
|
132
|
+
parameters: tool.inputSchema || { type: 'object', properties: {} },
|
|
133
|
+
handler: async (args: any) => {
|
|
134
|
+
console.log(`[AguiAdapter] Executing MCP tool: ${tool.name}`, args);
|
|
135
|
+
const result = await client.callTool(tool.name, args);
|
|
136
|
+
|
|
137
|
+
// Extract text content from result
|
|
138
|
+
if (result.content && Array.isArray(result.content)) {
|
|
139
|
+
const textContent = result.content
|
|
140
|
+
.filter((c: any) => c.type === 'text')
|
|
141
|
+
.map((c: any) => c.text)
|
|
142
|
+
.join('\n');
|
|
143
|
+
return textContent || result;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return result;
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return tools;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Get tools as a function (for dynamic loading).
|
|
156
|
+
*
|
|
157
|
+
* @returns Function that returns a Promise of tools
|
|
158
|
+
*/
|
|
159
|
+
getToolsFunction(): () => Promise<AguiTool[]> {
|
|
160
|
+
return async () => this.getTools();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Get tool definitions in JSON Schema format for passing to remote agents.
|
|
165
|
+
*
|
|
166
|
+
* This format is compatible with:
|
|
167
|
+
* - OpenAI's function calling API
|
|
168
|
+
* - AG-UI input.tools format
|
|
169
|
+
* - Most LLM tool/function calling implementations
|
|
170
|
+
*
|
|
171
|
+
* @returns Array of AguiToolDefinition objects
|
|
172
|
+
*/
|
|
173
|
+
async getToolDefinitions(): Promise<AguiToolDefinition[]> {
|
|
174
|
+
const isMultiSession = typeof (this.client as any).getClients === 'function';
|
|
175
|
+
|
|
176
|
+
if (isMultiSession) {
|
|
177
|
+
const clients = (this.client as MultiSessionClient).getClients();
|
|
178
|
+
const allTools: AguiToolDefinition[] = [];
|
|
179
|
+
|
|
180
|
+
for (const client of clients) {
|
|
181
|
+
const tools = await this.transformToolDefinitions(client);
|
|
182
|
+
allTools.push(...tools);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return allTools;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return this.transformToolDefinitions(this.client as MCPClient);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private async transformToolDefinitions(client: MCPClient): Promise<AguiToolDefinition[]> {
|
|
192
|
+
if (!client.isConnected()) {
|
|
193
|
+
return [];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const result = await client.listTools();
|
|
197
|
+
const prefix = this.options.prefix ?? client.getServerId() ?? 'mcp';
|
|
198
|
+
const tools: AguiToolDefinition[] = [];
|
|
199
|
+
|
|
200
|
+
for (const tool of result.tools) {
|
|
201
|
+
tools.push({
|
|
202
|
+
name: `${prefix}_${tool.name}`,
|
|
203
|
+
description: tool.description || `Execute ${tool.name}`,
|
|
204
|
+
parameters: tool.inputSchema || { type: 'object', properties: {} },
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return tools;
|
|
209
|
+
}
|
|
210
|
+
}
|