@tambo-ai/react 0.47.0 → 0.48.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/dist/context-helpers/current-interactables-context-helper.d.ts +28 -0
- package/dist/context-helpers/current-interactables-context-helper.d.ts.map +1 -0
- package/dist/context-helpers/current-interactables-context-helper.js +61 -0
- package/dist/context-helpers/current-interactables-context-helper.js.map +1 -0
- package/dist/context-helpers/index.d.ts +1 -0
- package/dist/context-helpers/index.d.ts.map +1 -1
- package/dist/context-helpers/index.js +1 -0
- package/dist/context-helpers/index.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/mcp/__tests__/tambo-mcp-provider.test.d.ts +2 -0
- package/dist/mcp/__tests__/tambo-mcp-provider.test.d.ts.map +1 -0
- package/dist/mcp/__tests__/tambo-mcp-provider.test.js +115 -0
- package/dist/mcp/__tests__/tambo-mcp-provider.test.js.map +1 -0
- package/dist/mcp/tambo-mcp-provider.d.ts +6 -0
- package/dist/mcp/tambo-mcp-provider.d.ts.map +1 -1
- package/dist/mcp/tambo-mcp-provider.js +25 -2
- package/dist/mcp/tambo-mcp-provider.js.map +1 -1
- package/dist/providers/__tests__/tambo-interactables-additional-context-edge-cases.test.d.ts +2 -0
- package/dist/providers/__tests__/tambo-interactables-additional-context-edge-cases.test.d.ts.map +1 -0
- package/dist/providers/__tests__/tambo-interactables-additional-context-edge-cases.test.js +339 -0
- package/dist/providers/__tests__/tambo-interactables-additional-context-edge-cases.test.js.map +1 -0
- package/dist/providers/__tests__/tambo-interactables-additional-context.test.d.ts +2 -0
- package/dist/providers/__tests__/tambo-interactables-additional-context.test.d.ts.map +1 -0
- package/dist/providers/__tests__/tambo-interactables-additional-context.test.js +299 -0
- package/dist/providers/__tests__/tambo-interactables-additional-context.test.js.map +1 -0
- package/dist/providers/tambo-interactable-provider.d.ts +20 -0
- package/dist/providers/tambo-interactable-provider.d.ts.map +1 -1
- package/dist/providers/tambo-interactable-provider.js +32 -2
- package/dist/providers/tambo-interactable-provider.js.map +1 -1
- package/esm/context-helpers/current-interactables-context-helper.d.ts +28 -0
- package/esm/context-helpers/current-interactables-context-helper.d.ts.map +1 -0
- package/esm/context-helpers/current-interactables-context-helper.js +56 -0
- package/esm/context-helpers/current-interactables-context-helper.js.map +1 -0
- package/esm/context-helpers/index.d.ts +1 -0
- package/esm/context-helpers/index.d.ts.map +1 -1
- package/esm/context-helpers/index.js +1 -0
- package/esm/context-helpers/index.js.map +1 -1
- package/esm/index.d.ts +1 -1
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +1 -1
- package/esm/index.js.map +1 -1
- package/esm/mcp/__tests__/tambo-mcp-provider.test.d.ts +2 -0
- package/esm/mcp/__tests__/tambo-mcp-provider.test.d.ts.map +1 -0
- package/esm/mcp/__tests__/tambo-mcp-provider.test.js +113 -0
- package/esm/mcp/__tests__/tambo-mcp-provider.test.js.map +1 -0
- package/esm/mcp/tambo-mcp-provider.d.ts +6 -0
- package/esm/mcp/tambo-mcp-provider.d.ts.map +1 -1
- package/esm/mcp/tambo-mcp-provider.js +24 -2
- package/esm/mcp/tambo-mcp-provider.js.map +1 -1
- package/esm/providers/__tests__/tambo-interactables-additional-context-edge-cases.test.d.ts +2 -0
- package/esm/providers/__tests__/tambo-interactables-additional-context-edge-cases.test.d.ts.map +1 -0
- package/esm/providers/__tests__/tambo-interactables-additional-context-edge-cases.test.js +334 -0
- package/esm/providers/__tests__/tambo-interactables-additional-context-edge-cases.test.js.map +1 -0
- package/esm/providers/__tests__/tambo-interactables-additional-context.test.d.ts +2 -0
- package/esm/providers/__tests__/tambo-interactables-additional-context.test.d.ts.map +1 -0
- package/esm/providers/__tests__/tambo-interactables-additional-context.test.js +294 -0
- package/esm/providers/__tests__/tambo-interactables-additional-context.test.js.map +1 -0
- package/esm/providers/tambo-interactable-provider.d.ts +20 -0
- package/esm/providers/tambo-interactable-provider.d.ts.map +1 -1
- package/esm/providers/tambo-interactable-provider.js +30 -1
- package/esm/providers/tambo-interactable-provider.js.map +1 -1
- package/package.json +8 -8
- package/dist/mcp/mcp-tools-client.d.ts +0 -96
- package/dist/mcp/mcp-tools-client.d.ts.map +0 -1
- package/dist/mcp/mcp-tools-client.js +0 -178
- package/dist/mcp/mcp-tools-client.js.map +0 -1
- package/esm/mcp/mcp-tools-client.d.ts +0 -96
- package/esm/mcp/mcp-tools-client.d.ts.map +0 -1
- package/esm/mcp/mcp-tools-client.js +0 -174
- package/esm/mcp/mcp-tools-client.js.map +0 -1
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Minimal Model Context Protocol (MCP) client using JSON-RPC 2.0
|
|
3
|
-
* Supports only listTools() and callTool() operations
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* Interface representing a Tool in the MCP
|
|
7
|
-
*/
|
|
8
|
-
export interface Tool {
|
|
9
|
-
name: string;
|
|
10
|
-
description?: string;
|
|
11
|
-
inputSchema: {
|
|
12
|
-
type: "object";
|
|
13
|
-
properties: Record<string, any>;
|
|
14
|
-
required?: string[];
|
|
15
|
-
};
|
|
16
|
-
annotations?: Record<string, any>;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Response from tools/list endpoint
|
|
20
|
-
*/
|
|
21
|
-
export interface ListToolsResult {
|
|
22
|
-
tools: Tool[];
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Content type for tool responses
|
|
26
|
-
*/
|
|
27
|
-
export interface ToolContent {
|
|
28
|
-
type: string;
|
|
29
|
-
text?: string;
|
|
30
|
-
annotations?: Record<string, any>;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Response from tools/call endpoint
|
|
34
|
-
*/
|
|
35
|
-
export interface CallToolResult {
|
|
36
|
-
content: ToolContent[];
|
|
37
|
-
isError?: boolean;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* A minimal TypeScript client for the Model Context Protocol (MCP)
|
|
41
|
-
*
|
|
42
|
-
* This client provides a streamlined interface to communicate with MCP servers
|
|
43
|
-
* using JSON-RPC 2.0 over HTTP. It supports listing available tools and calling tools
|
|
44
|
-
* with arguments, including support for Server-Sent Events (SSE) streaming responses.
|
|
45
|
-
* @example
|
|
46
|
-
* ```typescript
|
|
47
|
-
* // Basic usage
|
|
48
|
-
* const mcpClient = new MCPClient('https://example.com/mcp');
|
|
49
|
-
* const tools = await mcpClient.listTools();
|
|
50
|
-
* const toolResponse = await mcpClient.callTool('my-tool', { arg1: 'value1' });
|
|
51
|
-
*
|
|
52
|
-
* // For streaming responses:
|
|
53
|
-
* const stream = await mcpClient.callToolStream('streaming-tool', { arg1: 'value1' });
|
|
54
|
-
* for await (const chunk of mcpClient.parseStreamingResponses(stream)) {
|
|
55
|
-
* console.log('Received chunk:', chunk);
|
|
56
|
-
* }
|
|
57
|
-
* ```
|
|
58
|
-
*/
|
|
59
|
-
export declare class MCPClient {
|
|
60
|
-
private baseUrl;
|
|
61
|
-
private headers;
|
|
62
|
-
private requestId;
|
|
63
|
-
/**
|
|
64
|
-
* Creates a new MCP client
|
|
65
|
-
* @param url - The base URL of the MCP server
|
|
66
|
-
* @param extraHeaders - Optional additional headers to include in requests
|
|
67
|
-
*/
|
|
68
|
-
constructor(url: string, extraHeaders?: Record<string, string>);
|
|
69
|
-
/**
|
|
70
|
-
* Lists available tools on the MCP server
|
|
71
|
-
* @returns Promise resolving to the list of available tools
|
|
72
|
-
*/
|
|
73
|
-
listTools(): Promise<ListToolsResult>;
|
|
74
|
-
/**
|
|
75
|
-
* Calls a tool with the provided arguments
|
|
76
|
-
* @param toolName - The name of the tool to call
|
|
77
|
-
* @param args - The arguments to pass to the tool
|
|
78
|
-
* @returns Promise resolving to the tool's response
|
|
79
|
-
*/
|
|
80
|
-
callTool(toolName: string, args: any): Promise<CallToolResult>;
|
|
81
|
-
/**
|
|
82
|
-
* Calls a tool with the provided arguments and returns a stream of results
|
|
83
|
-
* @param toolName - The name of the tool to call
|
|
84
|
-
* @param args - The arguments to pass to the tool
|
|
85
|
-
* @returns ReadableStream of the tool's response
|
|
86
|
-
*/
|
|
87
|
-
callToolStream(toolName: string, args: any): Promise<ReadableStream<Uint8Array>>;
|
|
88
|
-
/**
|
|
89
|
-
* Parse SSE messages from a stream into JSON-RPC responses
|
|
90
|
-
* @param stream - ReadableStream to parse
|
|
91
|
-
* @returns AsyncGenerator yielding parsed JSON-RPC responses
|
|
92
|
-
* @yields {CallToolResult} The parsed JSON-RPC response
|
|
93
|
-
*/
|
|
94
|
-
parseStreamingResponses(stream: ReadableStream<Uint8Array>): AsyncGenerator<CallToolResult, void, unknown>;
|
|
95
|
-
}
|
|
96
|
-
//# sourceMappingURL=mcp-tools-client.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-tools-client.d.ts","sourceRoot":"","sources":["../../src/mcp/mcp-tools-client.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,MAAM,WAAW,IAAI;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE;QACX,IAAI,EAAE,QAAQ,CAAC;QACf,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QAChC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;KACrB,CAAC;IACF,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACnC;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,IAAI,EAAE,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACnC;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,WAAW,EAAE,CAAC;IACvB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,OAAO,CAAyB;IACxC,OAAO,CAAC,SAAS,CAAK;IAEtB;;;;OAIG;gBACS,GAAG,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAS9D;;;OAGG;IACG,SAAS,IAAI,OAAO,CAAC,eAAe,CAAC;IAgC3C;;;;;OAKG;IACG,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,cAAc,CAAC;IAmCpE;;;;;OAKG;IACG,cAAc,CAClB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,GAAG,GACR,OAAO,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC;IAkCtC;;;;;OAKG;IACI,uBAAuB,CAC5B,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,GACjC,cAAc,CAAC,cAAc,EAAE,IAAI,EAAE,OAAO,CAAC;CAwCjD"}
|
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Minimal Model Context Protocol (MCP) client using JSON-RPC 2.0
|
|
4
|
-
* Supports only listTools() and callTool() operations
|
|
5
|
-
*/
|
|
6
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.MCPClient = void 0;
|
|
8
|
-
/**
|
|
9
|
-
* A minimal TypeScript client for the Model Context Protocol (MCP)
|
|
10
|
-
*
|
|
11
|
-
* This client provides a streamlined interface to communicate with MCP servers
|
|
12
|
-
* using JSON-RPC 2.0 over HTTP. It supports listing available tools and calling tools
|
|
13
|
-
* with arguments, including support for Server-Sent Events (SSE) streaming responses.
|
|
14
|
-
* @example
|
|
15
|
-
* ```typescript
|
|
16
|
-
* // Basic usage
|
|
17
|
-
* const mcpClient = new MCPClient('https://example.com/mcp');
|
|
18
|
-
* const tools = await mcpClient.listTools();
|
|
19
|
-
* const toolResponse = await mcpClient.callTool('my-tool', { arg1: 'value1' });
|
|
20
|
-
*
|
|
21
|
-
* // For streaming responses:
|
|
22
|
-
* const stream = await mcpClient.callToolStream('streaming-tool', { arg1: 'value1' });
|
|
23
|
-
* for await (const chunk of mcpClient.parseStreamingResponses(stream)) {
|
|
24
|
-
* console.log('Received chunk:', chunk);
|
|
25
|
-
* }
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
class MCPClient {
|
|
29
|
-
baseUrl;
|
|
30
|
-
headers;
|
|
31
|
-
requestId = 1;
|
|
32
|
-
/**
|
|
33
|
-
* Creates a new MCP client
|
|
34
|
-
* @param url - The base URL of the MCP server
|
|
35
|
-
* @param extraHeaders - Optional additional headers to include in requests
|
|
36
|
-
*/
|
|
37
|
-
constructor(url, extraHeaders) {
|
|
38
|
-
this.baseUrl = url.endsWith("/") ? url : `${url}/`;
|
|
39
|
-
this.headers = {
|
|
40
|
-
"Content-Type": "application/json",
|
|
41
|
-
Accept: "application/json",
|
|
42
|
-
...(extraHeaders ?? {}),
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Lists available tools on the MCP server
|
|
47
|
-
* @returns Promise resolving to the list of available tools
|
|
48
|
-
*/
|
|
49
|
-
async listTools() {
|
|
50
|
-
const jsonRpcRequest = {
|
|
51
|
-
jsonrpc: "2.0",
|
|
52
|
-
method: "tools/list",
|
|
53
|
-
params: {},
|
|
54
|
-
id: this.requestId++,
|
|
55
|
-
};
|
|
56
|
-
const response = await fetch(this.baseUrl, {
|
|
57
|
-
method: "POST",
|
|
58
|
-
headers: this.headers,
|
|
59
|
-
body: JSON.stringify(jsonRpcRequest),
|
|
60
|
-
});
|
|
61
|
-
if (!response.ok) {
|
|
62
|
-
throw new Error(`Failed to list tools: ${response.status} ${response.statusText}`);
|
|
63
|
-
}
|
|
64
|
-
const jsonRpcResponse = await response.json();
|
|
65
|
-
// Handle JSON-RPC error
|
|
66
|
-
if (jsonRpcResponse.error) {
|
|
67
|
-
throw new Error(`JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`);
|
|
68
|
-
}
|
|
69
|
-
return jsonRpcResponse.result;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Calls a tool with the provided arguments
|
|
73
|
-
* @param toolName - The name of the tool to call
|
|
74
|
-
* @param args - The arguments to pass to the tool
|
|
75
|
-
* @returns Promise resolving to the tool's response
|
|
76
|
-
*/
|
|
77
|
-
async callTool(toolName, args) {
|
|
78
|
-
const jsonRpcRequest = {
|
|
79
|
-
jsonrpc: "2.0",
|
|
80
|
-
method: "tools/call",
|
|
81
|
-
params: {
|
|
82
|
-
name: toolName,
|
|
83
|
-
arguments: args,
|
|
84
|
-
},
|
|
85
|
-
id: this.requestId++,
|
|
86
|
-
};
|
|
87
|
-
const response = await fetch(this.baseUrl, {
|
|
88
|
-
method: "POST",
|
|
89
|
-
headers: this.headers,
|
|
90
|
-
body: JSON.stringify(jsonRpcRequest),
|
|
91
|
-
});
|
|
92
|
-
if (!response.ok) {
|
|
93
|
-
throw new Error(`Failed to call tool ${toolName}: ${response.status} ${response.statusText}`);
|
|
94
|
-
}
|
|
95
|
-
const jsonRpcResponse = await response.json();
|
|
96
|
-
// Handle JSON-RPC error
|
|
97
|
-
if (jsonRpcResponse.error) {
|
|
98
|
-
throw new Error(`JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`);
|
|
99
|
-
}
|
|
100
|
-
return jsonRpcResponse.result;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Calls a tool with the provided arguments and returns a stream of results
|
|
104
|
-
* @param toolName - The name of the tool to call
|
|
105
|
-
* @param args - The arguments to pass to the tool
|
|
106
|
-
* @returns ReadableStream of the tool's response
|
|
107
|
-
*/
|
|
108
|
-
async callToolStream(toolName, args) {
|
|
109
|
-
const jsonRpcRequest = {
|
|
110
|
-
jsonrpc: "2.0",
|
|
111
|
-
method: "tools/call",
|
|
112
|
-
params: {
|
|
113
|
-
name: toolName,
|
|
114
|
-
arguments: args,
|
|
115
|
-
},
|
|
116
|
-
id: this.requestId++,
|
|
117
|
-
};
|
|
118
|
-
const response = await fetch(this.baseUrl, {
|
|
119
|
-
method: "POST",
|
|
120
|
-
headers: {
|
|
121
|
-
...this.headers,
|
|
122
|
-
Accept: "text/event-stream",
|
|
123
|
-
},
|
|
124
|
-
body: JSON.stringify(jsonRpcRequest),
|
|
125
|
-
});
|
|
126
|
-
if (!response.ok) {
|
|
127
|
-
throw new Error(`Failed to call tool ${toolName}: ${response.status} ${response.statusText}`);
|
|
128
|
-
}
|
|
129
|
-
if (!response.body) {
|
|
130
|
-
throw new Error("Response body is null");
|
|
131
|
-
}
|
|
132
|
-
// Return the response body as a stream
|
|
133
|
-
return response.body;
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Parse SSE messages from a stream into JSON-RPC responses
|
|
137
|
-
* @param stream - ReadableStream to parse
|
|
138
|
-
* @returns AsyncGenerator yielding parsed JSON-RPC responses
|
|
139
|
-
* @yields {CallToolResult} The parsed JSON-RPC response
|
|
140
|
-
*/
|
|
141
|
-
async *parseStreamingResponses(stream) {
|
|
142
|
-
const reader = stream.getReader();
|
|
143
|
-
const decoder = new TextDecoder();
|
|
144
|
-
let buffer = "";
|
|
145
|
-
try {
|
|
146
|
-
while (true) {
|
|
147
|
-
const { done, value } = await reader.read();
|
|
148
|
-
if (done)
|
|
149
|
-
break;
|
|
150
|
-
buffer += decoder.decode(value, { stream: true });
|
|
151
|
-
// Process complete SSE messages
|
|
152
|
-
const lines = buffer.split("\n\n");
|
|
153
|
-
buffer = lines.pop() ?? "";
|
|
154
|
-
for (const line of lines) {
|
|
155
|
-
if (line.startsWith("data: ")) {
|
|
156
|
-
const jsonData = line.slice(6).trim();
|
|
157
|
-
try {
|
|
158
|
-
const jsonRpcResponse = JSON.parse(jsonData);
|
|
159
|
-
// Handle JSON-RPC error
|
|
160
|
-
if (jsonRpcResponse.error) {
|
|
161
|
-
throw new Error(`JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`);
|
|
162
|
-
}
|
|
163
|
-
yield jsonRpcResponse.result;
|
|
164
|
-
}
|
|
165
|
-
catch (e) {
|
|
166
|
-
console.error("Failed to parse JSON:", e);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
finally {
|
|
173
|
-
reader.releaseLock();
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
exports.MCPClient = MCPClient;
|
|
178
|
-
//# sourceMappingURL=mcp-tools-client.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-tools-client.js","sourceRoot":"","sources":["../../src/mcp/mcp-tools-client.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAwCH;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAa,SAAS;IACZ,OAAO,CAAS;IAChB,OAAO,CAAyB;IAChC,SAAS,GAAG,CAAC,CAAC;IAEtB;;;;OAIG;IACH,YAAY,GAAW,EAAE,YAAqC;QAC5D,IAAI,CAAC,OAAO,GAAG,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,CAAC;QACnD,IAAI,CAAC,OAAO,GAAG;YACb,cAAc,EAAE,kBAAkB;YAClC,MAAM,EAAE,kBAAkB;YAC1B,GAAG,CAAC,YAAY,IAAI,EAAE,CAAC;SACxB,CAAC;IACJ,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,SAAS;QACb,MAAM,cAAc,GAAG;YAC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AAC;wBAC/B,CAAC;wBAAC,OAAO,CAAC,EAAE,CAAC;4BACX,OAAO,CAAC,KAAK,CAAC,uBAAuB,EAAE,CAAC,CAAC,CAAC;wBAC5C,CAAC;oBACH,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,MAAM,CAAC,WAAW,EAAE,CAAC;QACvB,CAAC;IACH,CAAC;CACF;AA3LD,8BA2LC","sourcesContent":["/**\n * Minimal Model Context Protocol (MCP) client using JSON-RPC 2.0\n * Supports only listTools() and callTool() operations\n */\n\n/**\n * Interface representing a Tool in the MCP\n */\nexport interface Tool {\n name: string;\n description?: string;\n inputSchema: {\n type: \"object\";\n properties: Record<string, any>;\n required?: string[];\n };\n annotations?: Record<string, any>;\n}\n\n/**\n * Response from tools/list endpoint\n */\nexport interface ListToolsResult {\n tools: Tool[];\n}\n\n/**\n * Content type for tool responses\n */\nexport interface ToolContent {\n type: string;\n text?: string;\n annotations?: Record<string, any>;\n}\n\n/**\n * Response from tools/call endpoint\n */\nexport interface CallToolResult {\n content: ToolContent[];\n isError?: boolean;\n}\n\n/**\n * A minimal TypeScript client for the Model Context Protocol (MCP)\n *\n * This client provides a streamlined interface to communicate with MCP servers\n * using JSON-RPC 2.0 over HTTP. It supports listing available tools and calling tools\n * with arguments, including support for Server-Sent Events (SSE) streaming responses.\n * @example\n * ```typescript\n * // Basic usage\n * const mcpClient = new MCPClient('https://example.com/mcp');\n * const tools = await mcpClient.listTools();\n * const toolResponse = await mcpClient.callTool('my-tool', { arg1: 'value1' });\n *\n * // For streaming responses:\n * const stream = await mcpClient.callToolStream('streaming-tool', { arg1: 'value1' });\n * for await (const chunk of mcpClient.parseStreamingResponses(stream)) {\n * console.log('Received chunk:', chunk);\n * }\n * ```\n */\nexport class MCPClient {\n private baseUrl: string;\n private headers: Record<string, string>;\n private requestId = 1;\n\n /**\n * Creates a new MCP client\n * @param url - The base URL of the MCP server\n * @param extraHeaders - Optional additional headers to include in requests\n */\n constructor(url: string, extraHeaders?: Record<string, string>) {\n this.baseUrl = url.endsWith(\"/\") ? url : `${url}/`;\n this.headers = {\n \"Content-Type\": \"application/json\",\n Accept: \"application/json\",\n ...(extraHeaders ?? {}),\n };\n }\n\n /**\n * Lists available tools on the MCP server\n * @returns Promise resolving to the list of available tools\n */\n async listTools(): Promise<ListToolsResult> {\n const jsonRpcRequest = {\n jsonrpc: \"2.0\",\n method: \"tools/list\",\n params: {},\n id: this.requestId++,\n };\n\n const response = await fetch(this.baseUrl, {\n method: \"POST\",\n headers: this.headers,\n body: JSON.stringify(jsonRpcRequest),\n });\n\n if (!response.ok) {\n throw new Error(\n `Failed to list tools: ${response.status} ${response.statusText}`,\n );\n }\n\n const jsonRpcResponse = await response.json();\n\n // Handle JSON-RPC error\n if (jsonRpcResponse.error) {\n throw new Error(\n `JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`,\n );\n }\n\n return jsonRpcResponse.result;\n }\n\n /**\n * Calls a tool with the provided arguments\n * @param toolName - The name of the tool to call\n * @param args - The arguments to pass to the tool\n * @returns Promise resolving to the tool's response\n */\n async callTool(toolName: string, args: any): Promise<CallToolResult> {\n const jsonRpcRequest = {\n jsonrpc: \"2.0\",\n method: \"tools/call\",\n params: {\n name: toolName,\n arguments: args,\n },\n id: this.requestId++,\n };\n\n const response = await fetch(this.baseUrl, {\n method: \"POST\",\n headers: this.headers,\n body: JSON.stringify(jsonRpcRequest),\n });\n\n if (!response.ok) {\n throw new Error(\n `Failed to call tool ${toolName}: ${response.status} ${response.statusText}`,\n );\n }\n\n const jsonRpcResponse = await response.json();\n\n // Handle JSON-RPC error\n if (jsonRpcResponse.error) {\n throw new Error(\n `JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`,\n );\n }\n\n return jsonRpcResponse.result;\n }\n\n /**\n * Calls a tool with the provided arguments and returns a stream of results\n * @param toolName - The name of the tool to call\n * @param args - The arguments to pass to the tool\n * @returns ReadableStream of the tool's response\n */\n async callToolStream(\n toolName: string,\n args: any,\n ): Promise<ReadableStream<Uint8Array>> {\n const jsonRpcRequest = {\n jsonrpc: \"2.0\",\n method: \"tools/call\",\n params: {\n name: toolName,\n arguments: args,\n },\n id: this.requestId++,\n };\n\n const response = await fetch(this.baseUrl, {\n method: \"POST\",\n headers: {\n ...this.headers,\n Accept: \"text/event-stream\",\n },\n body: JSON.stringify(jsonRpcRequest),\n });\n\n if (!response.ok) {\n throw new Error(\n `Failed to call tool ${toolName}: ${response.status} ${response.statusText}`,\n );\n }\n\n if (!response.body) {\n throw new Error(\"Response body is null\");\n }\n\n // Return the response body as a stream\n return response.body;\n }\n\n /**\n * Parse SSE messages from a stream into JSON-RPC responses\n * @param stream - ReadableStream to parse\n * @returns AsyncGenerator yielding parsed JSON-RPC responses\n * @yields {CallToolResult} The parsed JSON-RPC response\n */\n async *parseStreamingResponses(\n stream: ReadableStream<Uint8Array>,\n ): AsyncGenerator<CallToolResult, void, unknown> {\n const reader = stream.getReader();\n const decoder = new TextDecoder();\n let buffer = \"\";\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n\n buffer += decoder.decode(value, { stream: true });\n\n // Process complete SSE messages\n const lines = buffer.split(\"\\n\\n\");\n buffer = lines.pop() ?? \"\";\n\n for (const line of lines) {\n if (line.startsWith(\"data: \")) {\n const jsonData = line.slice(6).trim();\n try {\n const jsonRpcResponse = JSON.parse(jsonData);\n\n // Handle JSON-RPC error\n if (jsonRpcResponse.error) {\n throw new Error(\n `JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`,\n );\n }\n\n yield jsonRpcResponse.result;\n } catch (e) {\n console.error(\"Failed to parse JSON:\", e);\n }\n }\n }\n }\n } finally {\n reader.releaseLock();\n }\n }\n}\n"]}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Minimal Model Context Protocol (MCP) client using JSON-RPC 2.0
|
|
3
|
-
* Supports only listTools() and callTool() operations
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* Interface representing a Tool in the MCP
|
|
7
|
-
*/
|
|
8
|
-
export interface Tool {
|
|
9
|
-
name: string;
|
|
10
|
-
description?: string;
|
|
11
|
-
inputSchema: {
|
|
12
|
-
type: "object";
|
|
13
|
-
properties: Record<string, any>;
|
|
14
|
-
required?: string[];
|
|
15
|
-
};
|
|
16
|
-
annotations?: Record<string, any>;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Response from tools/list endpoint
|
|
20
|
-
*/
|
|
21
|
-
export interface ListToolsResult {
|
|
22
|
-
tools: Tool[];
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Content type for tool responses
|
|
26
|
-
*/
|
|
27
|
-
export interface ToolContent {
|
|
28
|
-
type: string;
|
|
29
|
-
text?: string;
|
|
30
|
-
annotations?: Record<string, any>;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Response from tools/call endpoint
|
|
34
|
-
*/
|
|
35
|
-
export interface CallToolResult {
|
|
36
|
-
content: ToolContent[];
|
|
37
|
-
isError?: boolean;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* A minimal TypeScript client for the Model Context Protocol (MCP)
|
|
41
|
-
*
|
|
42
|
-
* This client provides a streamlined interface to communicate with MCP servers
|
|
43
|
-
* using JSON-RPC 2.0 over HTTP. It supports listing available tools and calling tools
|
|
44
|
-
* with arguments, including support for Server-Sent Events (SSE) streaming responses.
|
|
45
|
-
* @example
|
|
46
|
-
* ```typescript
|
|
47
|
-
* // Basic usage
|
|
48
|
-
* const mcpClient = new MCPClient('https://example.com/mcp');
|
|
49
|
-
* const tools = await mcpClient.listTools();
|
|
50
|
-
* const toolResponse = await mcpClient.callTool('my-tool', { arg1: 'value1' });
|
|
51
|
-
*
|
|
52
|
-
* // For streaming responses:
|
|
53
|
-
* const stream = await mcpClient.callToolStream('streaming-tool', { arg1: 'value1' });
|
|
54
|
-
* for await (const chunk of mcpClient.parseStreamingResponses(stream)) {
|
|
55
|
-
* console.log('Received chunk:', chunk);
|
|
56
|
-
* }
|
|
57
|
-
* ```
|
|
58
|
-
*/
|
|
59
|
-
export declare class MCPClient {
|
|
60
|
-
private baseUrl;
|
|
61
|
-
private headers;
|
|
62
|
-
private requestId;
|
|
63
|
-
/**
|
|
64
|
-
* Creates a new MCP client
|
|
65
|
-
* @param url - The base URL of the MCP server
|
|
66
|
-
* @param extraHeaders - Optional additional headers to include in requests
|
|
67
|
-
*/
|
|
68
|
-
constructor(url: string, extraHeaders?: Record<string, string>);
|
|
69
|
-
/**
|
|
70
|
-
* Lists available tools on the MCP server
|
|
71
|
-
* @returns Promise resolving to the list of available tools
|
|
72
|
-
*/
|
|
73
|
-
listTools(): Promise<ListToolsResult>;
|
|
74
|
-
/**
|
|
75
|
-
* Calls a tool with the provided arguments
|
|
76
|
-
* @param toolName - The name of the tool to call
|
|
77
|
-
* @param args - The arguments to pass to the tool
|
|
78
|
-
* @returns Promise resolving to the tool's response
|
|
79
|
-
*/
|
|
80
|
-
callTool(toolName: string, args: any): Promise<CallToolResult>;
|
|
81
|
-
/**
|
|
82
|
-
* Calls a tool with the provided arguments and returns a stream of results
|
|
83
|
-
* @param toolName - The name of the tool to call
|
|
84
|
-
* @param args - The arguments to pass to the tool
|
|
85
|
-
* @returns ReadableStream of the tool's response
|
|
86
|
-
*/
|
|
87
|
-
callToolStream(toolName: string, args: any): Promise<ReadableStream<Uint8Array>>;
|
|
88
|
-
/**
|
|
89
|
-
* Parse SSE messages from a stream into JSON-RPC responses
|
|
90
|
-
* @param stream - ReadableStream to parse
|
|
91
|
-
* @returns AsyncGenerator yielding parsed JSON-RPC responses
|
|
92
|
-
* @yields {CallToolResult} The parsed JSON-RPC response
|
|
93
|
-
*/
|
|
94
|
-
parseStreamingResponses(stream: ReadableStream<Uint8Array>): AsyncGenerator<CallToolResult, void, unknown>;
|
|
95
|
-
}
|
|
96
|
-
//# sourceMappingURL=mcp-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,OAAO,CAAC,cAAc,CAAC;IAmCpE;;;;;OAKG;IACG,cAAc,CAClB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,GAAG,GACR,OAAO,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC;IAkCtC;;;;;OAKG;IACI,uBAAuB,CAC5B,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,GACjC,cAAc,CAAC,cAAc,EAAE,IAAI,EAAE,OAAO,CAAC;CAwCjD"}
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Minimal Model Context Protocol (MCP) client using JSON-RPC 2.0
|
|
3
|
-
* Supports only listTools() and callTool() operations
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* A minimal TypeScript client for the Model Context Protocol (MCP)
|
|
7
|
-
*
|
|
8
|
-
* This client provides a streamlined interface to communicate with MCP servers
|
|
9
|
-
* using JSON-RPC 2.0 over HTTP. It supports listing available tools and calling tools
|
|
10
|
-
* with arguments, including support for Server-Sent Events (SSE) streaming responses.
|
|
11
|
-
* @example
|
|
12
|
-
* ```typescript
|
|
13
|
-
* // Basic usage
|
|
14
|
-
* const mcpClient = new MCPClient('https://example.com/mcp');
|
|
15
|
-
* const tools = await mcpClient.listTools();
|
|
16
|
-
* const toolResponse = await mcpClient.callTool('my-tool', { arg1: 'value1' });
|
|
17
|
-
*
|
|
18
|
-
* // For streaming responses:
|
|
19
|
-
* const stream = await mcpClient.callToolStream('streaming-tool', { arg1: 'value1' });
|
|
20
|
-
* for await (const chunk of mcpClient.parseStreamingResponses(stream)) {
|
|
21
|
-
* console.log('Received chunk:', chunk);
|
|
22
|
-
* }
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
export class MCPClient {
|
|
26
|
-
baseUrl;
|
|
27
|
-
headers;
|
|
28
|
-
requestId = 1;
|
|
29
|
-
/**
|
|
30
|
-
* Creates a new MCP client
|
|
31
|
-
* @param url - The base URL of the MCP server
|
|
32
|
-
* @param extraHeaders - Optional additional headers to include in requests
|
|
33
|
-
*/
|
|
34
|
-
constructor(url, extraHeaders) {
|
|
35
|
-
this.baseUrl = url.endsWith("/") ? url : `${url}/`;
|
|
36
|
-
this.headers = {
|
|
37
|
-
"Content-Type": "application/json",
|
|
38
|
-
Accept: "application/json",
|
|
39
|
-
...(extraHeaders ?? {}),
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Lists available tools on the MCP server
|
|
44
|
-
* @returns Promise resolving to the list of available tools
|
|
45
|
-
*/
|
|
46
|
-
async listTools() {
|
|
47
|
-
const jsonRpcRequest = {
|
|
48
|
-
jsonrpc: "2.0",
|
|
49
|
-
method: "tools/list",
|
|
50
|
-
params: {},
|
|
51
|
-
id: this.requestId++,
|
|
52
|
-
};
|
|
53
|
-
const response = await fetch(this.baseUrl, {
|
|
54
|
-
method: "POST",
|
|
55
|
-
headers: this.headers,
|
|
56
|
-
body: JSON.stringify(jsonRpcRequest),
|
|
57
|
-
});
|
|
58
|
-
if (!response.ok) {
|
|
59
|
-
throw new Error(`Failed to list tools: ${response.status} ${response.statusText}`);
|
|
60
|
-
}
|
|
61
|
-
const jsonRpcResponse = await response.json();
|
|
62
|
-
// Handle JSON-RPC error
|
|
63
|
-
if (jsonRpcResponse.error) {
|
|
64
|
-
throw new Error(`JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`);
|
|
65
|
-
}
|
|
66
|
-
return jsonRpcResponse.result;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Calls a tool with the provided arguments
|
|
70
|
-
* @param toolName - The name of the tool to call
|
|
71
|
-
* @param args - The arguments to pass to the tool
|
|
72
|
-
* @returns Promise resolving to the tool's response
|
|
73
|
-
*/
|
|
74
|
-
async callTool(toolName, args) {
|
|
75
|
-
const jsonRpcRequest = {
|
|
76
|
-
jsonrpc: "2.0",
|
|
77
|
-
method: "tools/call",
|
|
78
|
-
params: {
|
|
79
|
-
name: toolName,
|
|
80
|
-
arguments: args,
|
|
81
|
-
},
|
|
82
|
-
id: this.requestId++,
|
|
83
|
-
};
|
|
84
|
-
const response = await fetch(this.baseUrl, {
|
|
85
|
-
method: "POST",
|
|
86
|
-
headers: this.headers,
|
|
87
|
-
body: JSON.stringify(jsonRpcRequest),
|
|
88
|
-
});
|
|
89
|
-
if (!response.ok) {
|
|
90
|
-
throw new Error(`Failed to call tool ${toolName}: ${response.status} ${response.statusText}`);
|
|
91
|
-
}
|
|
92
|
-
const jsonRpcResponse = await response.json();
|
|
93
|
-
// Handle JSON-RPC error
|
|
94
|
-
if (jsonRpcResponse.error) {
|
|
95
|
-
throw new Error(`JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`);
|
|
96
|
-
}
|
|
97
|
-
return jsonRpcResponse.result;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Calls a tool with the provided arguments and returns a stream of results
|
|
101
|
-
* @param toolName - The name of the tool to call
|
|
102
|
-
* @param args - The arguments to pass to the tool
|
|
103
|
-
* @returns ReadableStream of the tool's response
|
|
104
|
-
*/
|
|
105
|
-
async callToolStream(toolName, args) {
|
|
106
|
-
const jsonRpcRequest = {
|
|
107
|
-
jsonrpc: "2.0",
|
|
108
|
-
method: "tools/call",
|
|
109
|
-
params: {
|
|
110
|
-
name: toolName,
|
|
111
|
-
arguments: args,
|
|
112
|
-
},
|
|
113
|
-
id: this.requestId++,
|
|
114
|
-
};
|
|
115
|
-
const response = await fetch(this.baseUrl, {
|
|
116
|
-
method: "POST",
|
|
117
|
-
headers: {
|
|
118
|
-
...this.headers,
|
|
119
|
-
Accept: "text/event-stream",
|
|
120
|
-
},
|
|
121
|
-
body: JSON.stringify(jsonRpcRequest),
|
|
122
|
-
});
|
|
123
|
-
if (!response.ok) {
|
|
124
|
-
throw new Error(`Failed to call tool ${toolName}: ${response.status} ${response.statusText}`);
|
|
125
|
-
}
|
|
126
|
-
if (!response.body) {
|
|
127
|
-
throw new Error("Response body is null");
|
|
128
|
-
}
|
|
129
|
-
// Return the response body as a stream
|
|
130
|
-
return response.body;
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Parse SSE messages from a stream into JSON-RPC responses
|
|
134
|
-
* @param stream - ReadableStream to parse
|
|
135
|
-
* @returns AsyncGenerator yielding parsed JSON-RPC responses
|
|
136
|
-
* @yields {CallToolResult} The parsed JSON-RPC response
|
|
137
|
-
*/
|
|
138
|
-
async *parseStreamingResponses(stream) {
|
|
139
|
-
const reader = stream.getReader();
|
|
140
|
-
const decoder = new TextDecoder();
|
|
141
|
-
let buffer = "";
|
|
142
|
-
try {
|
|
143
|
-
while (true) {
|
|
144
|
-
const { done, value } = await reader.read();
|
|
145
|
-
if (done)
|
|
146
|
-
break;
|
|
147
|
-
buffer += decoder.decode(value, { stream: true });
|
|
148
|
-
// Process complete SSE messages
|
|
149
|
-
const lines = buffer.split("\n\n");
|
|
150
|
-
buffer = lines.pop() ?? "";
|
|
151
|
-
for (const line of lines) {
|
|
152
|
-
if (line.startsWith("data: ")) {
|
|
153
|
-
const jsonData = line.slice(6).trim();
|
|
154
|
-
try {
|
|
155
|
-
const jsonRpcResponse = JSON.parse(jsonData);
|
|
156
|
-
// Handle JSON-RPC error
|
|
157
|
-
if (jsonRpcResponse.error) {
|
|
158
|
-
throw new Error(`JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`);
|
|
159
|
-
}
|
|
160
|
-
yield jsonRpcResponse.result;
|
|
161
|
-
}
|
|
162
|
-
catch (e) {
|
|
163
|
-
console.error("Failed to parse JSON:", e);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
finally {
|
|
170
|
-
reader.releaseLock();
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
//# sourceMappingURL=mcp-tools-client.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
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AM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;gBACnC,MAAM,GAAG,KAAK,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC;gBAE3B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;oBACzB,IAAI,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;wBAC9B,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;wBACtC,IAAI,CAAC;4BACH,MAAM,eAAe,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;4BAE7C,wBAAwB;4BACxB,IAAI,eAAe,CAAC,KAAK,EAAE,CAAC;gCAC1B,MAAM,IAAI,KAAK,CACb,mBAAmB,eAAe,CAAC,KAAK,CAAC,IAAI,MAAM,eAAe,CAAC,KAAK,CAAC,OAAO,EAAE,CACnF,CAAC;4BACJ,CAAC;4BAED,MAAM,eAAe,CAAC,MAAM,CAAC;wBAC/B,CAAC;wBAAC,OAAO,CAAC,EAAE,CAAC;4BACX,OAAO,CAAC,KAAK,CAAC,uBAAuB,EAAE,CAAC,CAAC,CAAC;wBAC5C,CAAC;oBACH,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,MAAM,CAAC,WAAW,EAAE,CAAC;QACvB,CAAC;IACH,CAAC;CACF","sourcesContent":["/**\n * Minimal Model Context Protocol (MCP) client using JSON-RPC 2.0\n * Supports only listTools() and callTool() operations\n */\n\n/**\n * Interface representing a Tool in the MCP\n */\nexport interface Tool {\n name: string;\n description?: string;\n inputSchema: {\n type: \"object\";\n properties: Record<string, any>;\n required?: string[];\n };\n annotations?: Record<string, any>;\n}\n\n/**\n * Response from tools/list endpoint\n */\nexport interface ListToolsResult {\n tools: Tool[];\n}\n\n/**\n * Content type for tool responses\n */\nexport interface ToolContent {\n type: string;\n text?: string;\n annotations?: Record<string, any>;\n}\n\n/**\n * Response from tools/call endpoint\n */\nexport interface CallToolResult {\n content: ToolContent[];\n isError?: boolean;\n}\n\n/**\n * A minimal TypeScript client for the Model Context Protocol (MCP)\n *\n * This client provides a streamlined interface to communicate with MCP servers\n * using JSON-RPC 2.0 over HTTP. It supports listing available tools and calling tools\n * with arguments, including support for Server-Sent Events (SSE) streaming responses.\n * @example\n * ```typescript\n * // Basic usage\n * const mcpClient = new MCPClient('https://example.com/mcp');\n * const tools = await mcpClient.listTools();\n * const toolResponse = await mcpClient.callTool('my-tool', { arg1: 'value1' });\n *\n * // For streaming responses:\n * const stream = await mcpClient.callToolStream('streaming-tool', { arg1: 'value1' });\n * for await (const chunk of mcpClient.parseStreamingResponses(stream)) {\n * console.log('Received chunk:', chunk);\n * }\n * ```\n */\nexport class MCPClient {\n private baseUrl: string;\n private headers: Record<string, string>;\n private requestId = 1;\n\n /**\n * Creates a new MCP client\n * @param url - The base URL of the MCP server\n * @param extraHeaders - Optional additional headers to include in requests\n */\n constructor(url: string, extraHeaders?: Record<string, string>) {\n this.baseUrl = url.endsWith(\"/\") ? url : `${url}/`;\n this.headers = {\n \"Content-Type\": \"application/json\",\n Accept: \"application/json\",\n ...(extraHeaders ?? {}),\n };\n }\n\n /**\n * Lists available tools on the MCP server\n * @returns Promise resolving to the list of available tools\n */\n async listTools(): Promise<ListToolsResult> {\n const jsonRpcRequest = {\n jsonrpc: \"2.0\",\n method: \"tools/list\",\n params: {},\n id: this.requestId++,\n };\n\n const response = await fetch(this.baseUrl, {\n method: \"POST\",\n headers: this.headers,\n body: JSON.stringify(jsonRpcRequest),\n });\n\n if (!response.ok) {\n throw new Error(\n `Failed to list tools: ${response.status} ${response.statusText}`,\n );\n }\n\n const jsonRpcResponse = await response.json();\n\n // Handle JSON-RPC error\n if (jsonRpcResponse.error) {\n throw new Error(\n `JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`,\n );\n }\n\n return jsonRpcResponse.result;\n }\n\n /**\n * Calls a tool with the provided arguments\n * @param toolName - The name of the tool to call\n * @param args - The arguments to pass to the tool\n * @returns Promise resolving to the tool's response\n */\n async callTool(toolName: string, args: any): Promise<CallToolResult> {\n const jsonRpcRequest = {\n jsonrpc: \"2.0\",\n method: \"tools/call\",\n params: {\n name: toolName,\n arguments: args,\n },\n id: this.requestId++,\n };\n\n const response = await fetch(this.baseUrl, {\n method: \"POST\",\n headers: this.headers,\n body: JSON.stringify(jsonRpcRequest),\n });\n\n if (!response.ok) {\n throw new Error(\n `Failed to call tool ${toolName}: ${response.status} ${response.statusText}`,\n );\n }\n\n const jsonRpcResponse = await response.json();\n\n // Handle JSON-RPC error\n if (jsonRpcResponse.error) {\n throw new Error(\n `JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`,\n );\n }\n\n return jsonRpcResponse.result;\n }\n\n /**\n * Calls a tool with the provided arguments and returns a stream of results\n * @param toolName - The name of the tool to call\n * @param args - The arguments to pass to the tool\n * @returns ReadableStream of the tool's response\n */\n async callToolStream(\n toolName: string,\n args: any,\n ): Promise<ReadableStream<Uint8Array>> {\n const jsonRpcRequest = {\n jsonrpc: \"2.0\",\n method: \"tools/call\",\n params: {\n name: toolName,\n arguments: args,\n },\n id: this.requestId++,\n };\n\n const response = await fetch(this.baseUrl, {\n method: \"POST\",\n headers: {\n ...this.headers,\n Accept: \"text/event-stream\",\n },\n body: JSON.stringify(jsonRpcRequest),\n });\n\n if (!response.ok) {\n throw new Error(\n `Failed to call tool ${toolName}: ${response.status} ${response.statusText}`,\n );\n }\n\n if (!response.body) {\n throw new Error(\"Response body is null\");\n }\n\n // Return the response body as a stream\n return response.body;\n }\n\n /**\n * Parse SSE messages from a stream into JSON-RPC responses\n * @param stream - ReadableStream to parse\n * @returns AsyncGenerator yielding parsed JSON-RPC responses\n * @yields {CallToolResult} The parsed JSON-RPC response\n */\n async *parseStreamingResponses(\n stream: ReadableStream<Uint8Array>,\n ): AsyncGenerator<CallToolResult, void, unknown> {\n const reader = stream.getReader();\n const decoder = new TextDecoder();\n let buffer = \"\";\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n\n buffer += decoder.decode(value, { stream: true });\n\n // Process complete SSE messages\n const lines = buffer.split(\"\\n\\n\");\n buffer = lines.pop() ?? \"\";\n\n for (const line of lines) {\n if (line.startsWith(\"data: \")) {\n const jsonData = line.slice(6).trim();\n try {\n const jsonRpcResponse = JSON.parse(jsonData);\n\n // Handle JSON-RPC error\n if (jsonRpcResponse.error) {\n throw new Error(\n `JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`,\n );\n }\n\n yield jsonRpcResponse.result;\n } catch (e) {\n console.error(\"Failed to parse JSON:\", e);\n }\n }\n }\n }\n } finally {\n reader.releaseLock();\n }\n }\n}\n"]}
|