@mcp-ts/sdk 1.3.1 → 1.3.3

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.
Files changed (63) hide show
  1. package/README.md +371 -290
  2. package/dist/adapters/agui-adapter.d.mts +3 -3
  3. package/dist/adapters/agui-adapter.d.ts +3 -3
  4. package/dist/adapters/agui-middleware.d.mts +3 -3
  5. package/dist/adapters/agui-middleware.d.ts +3 -3
  6. package/dist/adapters/ai-adapter.d.mts +3 -3
  7. package/dist/adapters/ai-adapter.d.ts +3 -3
  8. package/dist/adapters/langchain-adapter.d.mts +3 -3
  9. package/dist/adapters/langchain-adapter.d.ts +3 -3
  10. package/dist/adapters/mastra-adapter.d.mts +3 -3
  11. package/dist/adapters/mastra-adapter.d.ts +3 -3
  12. package/dist/client/index.d.mts +10 -66
  13. package/dist/client/index.d.ts +10 -66
  14. package/dist/client/index.js +91 -173
  15. package/dist/client/index.js.map +1 -1
  16. package/dist/client/index.mjs +91 -173
  17. package/dist/client/index.mjs.map +1 -1
  18. package/dist/client/react.d.mts +15 -5
  19. package/dist/client/react.d.ts +15 -5
  20. package/dist/client/react.js +130 -182
  21. package/dist/client/react.js.map +1 -1
  22. package/dist/client/react.mjs +130 -182
  23. package/dist/client/react.mjs.map +1 -1
  24. package/dist/client/vue.d.mts +27 -7
  25. package/dist/client/vue.d.ts +27 -7
  26. package/dist/client/vue.js +131 -182
  27. package/dist/client/vue.js.map +1 -1
  28. package/dist/client/vue.mjs +131 -182
  29. package/dist/client/vue.mjs.map +1 -1
  30. package/dist/{events-BgeztGYZ.d.mts → events-CK3N--3g.d.mts} +2 -0
  31. package/dist/{events-BgeztGYZ.d.ts → events-CK3N--3g.d.ts} +2 -0
  32. package/dist/index.d.mts +3 -3
  33. package/dist/index.d.ts +3 -3
  34. package/dist/index.js +224 -258
  35. package/dist/index.js.map +1 -1
  36. package/dist/index.mjs +224 -258
  37. package/dist/index.mjs.map +1 -1
  38. package/dist/{multi-session-client-CxogNckF.d.mts → multi-session-client-DzjmT7FX.d.mts} +4 -10
  39. package/dist/{multi-session-client-cox_WXUj.d.ts → multi-session-client-FAFpUzZ4.d.ts} +4 -10
  40. package/dist/server/index.d.mts +18 -23
  41. package/dist/server/index.d.ts +18 -23
  42. package/dist/server/index.js +133 -85
  43. package/dist/server/index.js.map +1 -1
  44. package/dist/server/index.mjs +133 -85
  45. package/dist/server/index.mjs.map +1 -1
  46. package/dist/shared/index.d.mts +3 -3
  47. package/dist/shared/index.d.ts +3 -3
  48. package/dist/shared/index.js.map +1 -1
  49. package/dist/shared/index.mjs.map +1 -1
  50. package/dist/{types-CLccx9wW.d.mts → types-CW6lghof.d.mts} +6 -0
  51. package/dist/{types-CLccx9wW.d.ts → types-CW6lghof.d.ts} +6 -0
  52. package/package.json +1 -1
  53. package/src/client/core/sse-client.ts +354 -493
  54. package/src/client/react/index.ts +16 -16
  55. package/src/client/react/use-mcp-apps.tsx +214 -214
  56. package/src/client/react/use-mcp.ts +84 -19
  57. package/src/client/vue/use-mcp.ts +119 -44
  58. package/src/server/handlers/nextjs-handler.ts +207 -217
  59. package/src/server/handlers/sse-handler.ts +14 -0
  60. package/src/server/mcp/oauth-client.ts +48 -46
  61. package/src/server/storage/types.ts +12 -5
  62. package/src/shared/events.ts +2 -0
  63. package/src/shared/types.ts +6 -0
package/README.md CHANGED
@@ -1,149 +1,114 @@
1
- <div align="center">
2
- <img src="docs/static/img/mcp-ts-banner.svg" alt="MCP-TS Banner" width="100%" style="max-width: 1200px;" />
3
- </div>
4
-
5
- <div align="center">
6
- <a href="https://zonlabs.github.io/mcp-ts/#ag-ui-demo">
7
- <em>Watch AG-UI + LangChain demo</em>
8
- </a>
9
- </div>
10
- <br />
11
-
12
-
13
- <div align="center">
14
-
15
- | *Supported Frameworks* | *Agent Frameworks and Protocol* | *Storage Backends* |
16
- | :---: | :---: | :---: |
17
- | <img src="docs/static/img/framework/next.svg" width="35" height="35" /> <img src="docs/static/img/framework/node.svg" width="35" height="35" /> <img src="docs/static/img/framework/react.svg" width="35" height="35" /> <img src="docs/static/img/framework/vue.svg" width="35" height="35" /> <img src="docs/static/img/framework/express.svg" width="35" height="35" /> | <img src="docs/static/img/framework/vercel.svg" width="35" height="35" /> <img src="docs/static/img/agent-framework/langchain.svg" width="35" height="35" /> <img src="docs/static/img/agent-framework/mastra.svg" width="35" height="35" /> <img src="docs/static/img/agent-framework/agui.webp" width="35" height="35" /> | <img src="docs/static/img/storage-backend/redis.svg" width="35" height="35" /> <img src="docs/static/img/storage-backend/sqlite.svg" width="35" height="35" /> <img src="docs/static/img/storage-backend/filesystem.svg" width="35" height="35" /> <img src="docs/static/img/storage-backend/memory.svg" width="35" height="35" /> |
18
-
19
- </div>
20
-
21
- <p align="center">
22
- <a href="https://www.npmjs.com/package/@mcp-ts/sdk">
23
- <img src="https://img.shields.io/npm/v/@mcp-ts/sdk.svg" alt="npm version" />
24
- </a>
25
- <a href="https://zonlabs.github.io/mcp-ts/">
26
- <img src="https://img.shields.io/badge/docs-website-brightgreen.svg" alt="Documentation" />
27
- </a>
28
- <a href="https://opensource.org/licenses/MIT">
29
- <img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License: MIT" />
30
- </a>
31
- </p>
32
-
33
-
34
-
35
- ## Features
36
-
37
- - **SSE** - Server-Sent Events for connection state and observability updates
38
- - **Flexible Storage** - Redis, SQLite, File System, or In-Memory backends
39
- - **Serverless** - Works in serverless environments (Vercel, AWS Lambda, etc.)
40
- - **React Hook** - `useMcp` hook for easy React integration
41
- - **Vue Composable** - `useMcp` composable for Vue applications
42
- - **MCP Protocol** - Support for tools, prompts, and resources
43
- - **Agent Adapters** - Built-in adapters for AI SDK, LangChain, Mastra, and AG-UI
44
- - **MCP Apps Extension (SEP-1865)** - Interactive UI-driven tool interfaces
45
-
46
- ## Examples
47
-
48
- Check out working examples demonstrating the MCP Apps extension and agent integrations in the [examples/agents](examples/agents) directory.
49
-
50
- > Examples MCP Apps referred from [modelcontextprotocol/ext-apps](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples)
51
-
52
- <div align="center">
53
- <table cellspacing="5" cellpadding="0">
54
- <tr>
55
- <td width="50%">
56
- <img src="docs/static/img/mcp-apps-img-1.png" alt="MCP Apps 1" width="100%" />
57
- </td>
58
- <td width="50%">
59
- <img src="docs/static/img/mcp-apps-img-2.png" alt="MCP Apps 2" width="100%" />
60
- </td>
61
- </tr>
62
- </table>
63
- <p><em>Interactive UIs for MCP tools</em></p>
64
- </div>
65
-
66
- ## Inspiration
67
-
68
- > I got the idea for `@mcp-ts` while working on 🌐 **[MCP Assistant](https://mcp-assistant.in)**.
69
- While building custom storage for persistence, managing the flow became harder than it should have been.
70
- So I built this client to handle the heavy lifting of client applications and make agent interactions easier.
71
- That’s how `@mcp-ts` started.
72
-
73
- <br/>
74
-
75
- <div align="center">
76
- <img src="docs/static/img/mcp-assistant.png" alt="MCP Assistant" width="100%" />
77
- </div>
78
-
79
- <br/>
80
-
81
- ## Installation
82
-
83
- ```bash
84
- npm install @mcp-ts/sdk
85
- ```
86
-
87
- The package supports multiple storage backends out of the box:
88
- - **Memory** (default, no setup required)
89
- - **File** (local persistence)
90
- - **SQLite** (fast local persistence, requires `npm install better-sqlite3`)
91
- - **Redis** (production-ready, requires `npm install ioredis`)
92
-
93
- ## Quick Start
94
-
95
- ### Server-Side (Next.js)
96
-
97
- ```typescript
98
- // app/api/mcp/route.ts
99
- import { createNextMcpHandler } from '@mcp-ts/sdk/server';
100
-
101
- export const dynamic = 'force-dynamic';
102
- export const runtime = 'nodejs';
103
-
104
- export const { GET, POST } = createNextMcpHandler({
105
- authenticate: () => {
106
- // your logic here
107
- }
108
- });
109
- });
110
- ```
111
-
112
- ### Using with Vercel AI SDK
113
-
114
- For advanced usage with `ai` SDK (e.g., `streamText`), use `MultiSessionClient` to aggregate tools from multiple servers.
115
-
116
- ```typescript
117
- // app/api/chat/route.ts
118
- import { MultiSessionClient } from '@mcp-ts/sdk/server';
119
- import { AIAdapter } from '@mcp-ts/sdk/adapters/ai';
120
- import { streamText } from 'ai';
121
- import { openai } from '@ai-sdk/openai';
122
-
123
- export async function POST(req: Request) {
124
- const { messages, identity } = await req.json();
125
- const client = new MultiSessionClient(identity);
126
-
127
- try {
128
- await client.connect();
129
- const tools = await AIAdapter.getTools(client);
130
- const result = streamText({
131
- model: openai('gpt-4'),
132
- messages,
133
- tools,
134
- onFinish: async () => {
135
- await mcp.disconnect();
136
- }
137
- });
138
- return result.toDataStreamResponse();
139
- } catch (error) {
140
- await mcp.disconnect();
141
- throw error;
142
- }
143
- }
144
- ```
145
-
146
- ### Client-Side (React)
1
+ <div align="center">
2
+ <img src="docs/static/img/mcp-ts-banner.svg" alt="MCP-TS Banner" width="100%" style="max-width: 1200px;" />
3
+ </div>
4
+
5
+ <div align="center">
6
+ <a href="https://zonlabs.github.io/mcp-ts/#ag-ui-demo">
7
+ <em>Watch AG-UI + LangChain demo</em>
8
+ </a>
9
+ </div>
10
+ <br />
11
+
12
+
13
+ <div align="center">
14
+
15
+ | *Supported Frameworks* | *Agent Frameworks and Protocol* | *Storage Backends* |
16
+ | :---: | :---: | :---: |
17
+ | <img src="docs/static/img/framework/next.svg" width="35" height="35" /> <img src="docs/static/img/framework/node.svg" width="35" height="35" /> <img src="docs/static/img/framework/react.svg" width="35" height="35" /> <img src="docs/static/img/framework/vue.svg" width="35" height="35" /> <img src="docs/static/img/framework/express.svg" width="35" height="35" /> | <img src="docs/static/img/framework/vercel.svg" width="35" height="35" /> <img src="docs/static/img/agent-framework/langchain.svg" width="35" height="35" /> <img src="docs/static/img/agent-framework/mastra.svg" width="35" height="35" /> <img src="docs/static/img/agent-framework/agui.webp" width="35" height="35" /> | <img src="docs/static/img/storage-backend/redis.svg" width="35" height="35" /> <img src="docs/static/img/storage-backend/sqlite.svg" width="35" height="35" /> <img src="docs/static/img/storage-backend/filesystem.svg" width="35" height="35" /> <img src="docs/static/img/storage-backend/memory.svg" width="35" height="35" /> |
18
+
19
+ </div>
20
+
21
+ <p align="center">
22
+ <a href="https://www.npmjs.com/package/@mcp-ts/sdk">
23
+ <img src="https://img.shields.io/npm/v/@mcp-ts/sdk?style=flat-square&logo=npm&logoColor=white&label=%40mcp-ts%2Fsdk&color=dc2626" alt="npm version" />
24
+ </a>
25
+ <a href="https://zonlabs.github.io/mcp-ts/">
26
+ <img src="https://img.shields.io/badge/docs-website-2563eb?style=flat-square&logo=readthedocs&logoColor=white" alt="Documentation" />
27
+ </a>
28
+ <a href="https://opensource.org/licenses/MIT">
29
+ <img src="https://img.shields.io/badge/license-MIT-84cc16?style=flat-square" alt="License: MIT" />
30
+ </a>
31
+ </p>
32
+
33
+
34
+
35
+ ## Features
36
+
37
+ - **SSE** - Server-Sent Events for connection state and observability updates
38
+ - **Flexible Storage** - Redis, SQLite, File System, or In-Memory backends
39
+ - **Serverless** - Works in serverless environments (Vercel, AWS Lambda, etc.)
40
+ - **React Hook** - `useMcp` hook for easy React integration
41
+ - **Vue Composable** - `useMcp` composable for Vue applications
42
+ - **MCP Protocol** - Support for tools, prompts, and resources
43
+ - **Agent Adapters** - Built-in adapters for AI SDK, LangChain, Mastra, and AG-UI
44
+ - **MCP Apps Extension (SEP-1865)** - Interactive UI-driven tool interfaces
45
+
46
+ ## 🧪 Examples
47
+
48
+ Check out working examples demonstrating the MCP Apps extension and agent integrations in the [examples/agents](examples/agents) directory.
49
+
50
+ > Examples MCP Apps referred from [modelcontextprotocol/ext-apps](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples)
51
+
52
+ <div align="center">
53
+ <table cellspacing="5" cellpadding="0">
54
+ <tr>
55
+ <td width="50%">
56
+ <img src="docs/static/img/mcp-apps-img-1.png" alt="MCP Apps 1" width="100%" />
57
+ </td>
58
+ <td width="50%">
59
+ <img src="docs/static/img/mcp-apps-img-2.png" alt="MCP Apps 2" width="100%" />
60
+ </td>
61
+ </tr>
62
+ </table>
63
+ <p><em>Interactive UIs for MCP tools</em></p>
64
+ </div>
65
+
66
+ ## 💡 Inspiration
67
+
68
+ > I got the idea for `@mcp-ts` while working on 🌐 **[MCP Assistant](https://mcp-assistant.in)**.
69
+ As the project grew, I had a few problems: storage, using different AI frameworks like LangGraph and ADK for different use cases, and figuring out how to get progressive SSE updates at each step so I could see what was happening.
70
+ So with that idea in mind, I built this SDK to make setup easier and keep the user experience smooth.
71
+ That’s how `@mcp-ts` started.
72
+
73
+ <br/>
74
+
75
+ <div align="center">
76
+ <img src="docs/static/img/mcp-assistant.png" alt="MCP Assistant" width="100%" />
77
+ </div>
78
+
79
+ <br/>
80
+
81
+ ## 📦 Installation
82
+
83
+ ```bash
84
+ npm install @mcp-ts/sdk
85
+ ```
86
+
87
+ The package supports multiple storage backends out of the box:
88
+ - **Memory** (default, no setup required)
89
+ - **File** (local persistence)
90
+ - **SQLite** (fast local persistence, requires `npm install better-sqlite3`)
91
+ - **Redis** (production-ready, requires `npm install ioredis`)
92
+
93
+ ## 🚀 Quick Start
94
+
95
+ ### 🖥️ Server-Side (Next.js)
96
+
97
+ ```typescript
98
+ // app/api/mcp/route.ts
99
+ import { createNextMcpHandler } from '@mcp-ts/sdk/server';
100
+
101
+ export const dynamic = 'force-dynamic';
102
+ export const runtime = 'nodejs';
103
+
104
+ export const { GET, POST } = createNextMcpHandler({
105
+ authenticate: () => {
106
+ // your logic here
107
+ }
108
+ });
109
+ ```
110
+
111
+ ### 🎯 Client-Side (React)
147
112
 
148
113
  ```typescript
149
114
  'use client';
@@ -151,15 +116,13 @@ export async function POST(req: Request) {
151
116
  import { useMcp } from '@mcp-ts/sdk/client/react';
152
117
 
153
118
  function App() {
154
- const { connections, connect, status } = useMcp({
119
+ const { connections, connect } = useMcp({
155
120
  url: '/api/mcp',
156
121
  identity: 'user-123',
157
122
  });
158
123
 
159
124
  return (
160
- <>
161
- <p>Status: {status}</p>
162
-
125
+ <div className="flex flex-col items-center gap-4">
163
126
  <button
164
127
  onClick={() =>
165
128
  connect({
@@ -172,7 +135,6 @@ function App() {
172
135
  >
173
136
  Connect
174
137
  </button>
175
-
176
138
  {connections.map((conn) => (
177
139
  <div key={conn.sessionId}>
178
140
  <h3>{conn.serverName}</h3>
@@ -180,145 +142,264 @@ function App() {
180
142
  <p>Tools: {conn.tools.length}</p>
181
143
  </div>
182
144
  ))}
183
- </>
145
+ </div>
184
146
  );
185
147
  }
186
148
  ```
187
-
188
- ### <img src="docs/static/img/agent-framework/agui.webp" width="20" height="20" align="center" /> AG-UI Middleware
189
-
190
- Execute MCP tools server-side when using remote agents (LangGraph, AutoGen, etc.):
191
-
192
- ```typescript
193
- import { HttpAgent } from "@ag-ui/client";
194
- import { AguiAdapter } from "@mcp-ts/sdk/adapters/agui-adapter";
195
- import { createMcpMiddleware } from "@mcp-ts/sdk/adapters/agui-middleware";
196
-
197
- // Connect to MCP servers
198
- const { MultiSessionClient } = await import("@mcp-ts/sdk/server");
199
- const client = new MultiSessionClient("user_123");
200
- await client.connect();
201
-
202
- // Create adapter and get tools
203
- const adapter = new AguiAdapter(client);
204
- const mcpTools = await adapter.getTools();
205
-
206
- // Create agent with middleware
207
- const agent = new HttpAgent({ url: "http://localhost:8000/agent" });
208
- agent.use(createMcpMiddleware({
209
- toolPrefix: 'server-',
210
- tools: mcpTools,
211
- }));
212
- ```
213
-
214
- The middleware intercepts tool calls from remote agents, executes MCP tools server-side, and returns results back to the agent.
215
-
216
- ## Documentation
217
-
218
- Full documentation is available at: **[Docs](https://zonlabs.github.io/mcp-ts/)**
219
-
220
- ### Topics Covered:
221
-
222
- - **[Getting Started](https://zonlabs.github.io/mcp-ts/docs/)** - Quick setup and overview
223
- - **[Installation](https://zonlabs.github.io/mcp-ts/docs/installation)** - Detailed installation guide
224
- - **[Storage Backends](https://zonlabs.github.io/mcp-ts/docs/storage-backends)** - Redis, File, Memory options
225
- - **[Next.js Integration](https://zonlabs.github.io/mcp-ts/docs/nextjs)** - Complete Next.js examples
226
- - **[React Hook Guide](https://zonlabs.github.io/mcp-ts/docs/react-hook)** - Using the useMcp hook
227
- - **[API Reference](https://zonlabs.github.io/mcp-ts/docs/api-reference)** - Complete API documentation
228
-
229
- ## Environment Setup
230
-
231
- The library supports multiple storage backends. You can explicitly select one using `MCP_TS_STORAGE_TYPE` or rely on automatic detection.
232
-
233
- **Supported Types:** `redis`, `sqlite`, `file`, `memory`.
234
-
235
- ### Configuration Examples
236
-
237
- 1. **<img src="docs/static/img/storage-backend/redis.svg" width="20" height="20" align="center" /> Redis** (Recommended for production)
238
- ```bash
239
- MCP_TS_STORAGE_TYPE=redis
240
- REDIS_URL=redis://localhost:6379
241
- ```
242
-
243
- 2. **<img src="docs/static/img/storage-backend/sqlite.svg" width="20" height="20" align="center" /> SQLite** (Fast & Persistent)
244
- ```bash
245
- MCP_TS_STORAGE_TYPE=sqlite
246
- # Optional path
247
- MCP_TS_STORAGE_SQLITE_PATH=./sessions.db
248
- ```
249
-
250
- 3. **<img src="docs/static/img/storage-backend/filesystem.svg" width="20" height="20" align="center" /> File System** (Great for local dev)
251
- ```bash
252
- MCP_TS_STORAGE_TYPE=file
253
- MCP_TS_STORAGE_FILE=./sessions.json
254
- ```
255
-
256
- 4. **<img src="docs/static/img/storage-backend/memory.svg" width="20" height="20" align="center" /> In-Memory** (Default for testing)
257
- ```bash
258
- MCP_TS_STORAGE_TYPE=memory
259
- ```
260
-
261
- ## Architecture
262
-
263
- This package uses **Server-Sent Events (SSE)** instead of WebSockets:
264
-
265
-
266
- ```mermaid
267
- graph TD
268
- subgraph Client ["Browser (React)"]
269
- UI[UI Components]
270
- Hook[useMcp Hook]
271
- UI <--> Hook
272
- end
273
-
274
- subgraph Server ["Next.js Server (Node.js)"]
275
- API[API Route /api/mcp]
276
- SSE[SSE Handler]
277
- ClientMgr[MCP Client Manager]
278
-
279
- API <--> ClientMgr
280
- ClientMgr --> SSE
281
- end
282
-
283
- subgraph Infrastructure
284
- Redis[(Redis Session Store)]
285
- end
286
-
287
- subgraph External ["External MCP Servers"]
288
- TargetServer[Target MCP Server]
289
- end
290
-
291
- Hook -- "HTTP POST (RPC)" --> API
292
- SSE -- "Server-Sent Events" --> Hook
293
- ClientMgr -- "Persist State" <--> Redis
294
- ClientMgr -- "MCP Protocol" <--> TargetServer
295
- ```
296
-
297
- - **Browser**: React application using the `useMcp` hook for state management.
298
- - **Next.js Server**: Acts as a bridge, maintaining connections to external MCP servers.
299
- - **Storage**: Persists session state, OAuth tokens, and connection details (Redis, File, or Memory).
300
- - **SSE**: Delivers real-time updates (logs, tool list changes) to the client.
301
-
302
- > [!NOTE]
303
- > This package (`@mcp-ts/sdk`) provides a unified MCP client with support for adapters and storage backends such as AI SDK, Mastra, LangChain, and Redis.
304
- > Adapters and storage backends are loaded via **optional peer dependencies** and must be installed independently. This ensures your application only includes the integrations you explicitly choose, keeping bundle size small and avoiding unnecessary dependencies.
305
- > The SDK includes built-in support for **Memory** and **File** storage, while additional backends (such as Redis) and adapters can be added without impacting users who don’t need them.
306
-
307
- For more details, refer to the documentation and follow the **installation guide for each adapter or storage backend**.
308
-
309
- - [AI SDK Installation Guide](https://zonlabs.github.io/mcp-ts/docs/adapters#installation)
310
- - [Mastra Installation Guide](https://zonlabs.github.io/mcp-ts/docs/adapters#installation)
311
- - [LangChain Installation Guide](https://zonlabs.github.io/mcp-ts/docs/adapters#installation)
312
- - [Redis Storage Installation Guide](https://zonlabs.github.io/mcp-ts/docs/storage-backends#-redis-production)
313
-
314
-
315
- ## Contributing
316
-
317
- Contributions are welcome! Please read [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines on how to contribute.
318
-
319
- ## License
320
-
321
- MIT © MCP Assistant
322
-
323
-
324
-
149
+
150
+ ### 🔌 Adapters
151
+
152
+ Integrating with agent frameworks is simple using built-in adapters.
153
+
154
+ <details>
155
+ <summary>Vercel AI SDK</summary>
156
+
157
+ ```typescript
158
+ // app/api/chat/route.ts
159
+ import { MultiSessionClient } from '@mcp-ts/sdk/server';
160
+ import { AIAdapter } from '@mcp-ts/sdk/adapters/ai';
161
+ import { streamText } from 'ai';
162
+ import { openai } from '@ai-sdk/openai';
163
+
164
+ export async function POST(req: Request) {
165
+ const { messages, identity } = await req.json();
166
+ const client = new MultiSessionClient(identity);
167
+
168
+ try {
169
+ await client.connect();
170
+ const tools = await AIAdapter.getTools(client);
171
+ const result = streamText({
172
+ model: openai('gpt-4'),
173
+ messages,
174
+ tools,
175
+ onFinish: async () => {
176
+ await client.disconnect();
177
+ }
178
+ });
179
+ return result.toDataStreamResponse();
180
+ } catch (error) {
181
+ await client.disconnect();
182
+ throw error;
183
+ }
184
+ }
185
+ ```
186
+
187
+ </details>
188
+
189
+ <details>
190
+ <summary>Agui Adapter</summary>
191
+
192
+ ```typescript
193
+ import { MultiSessionClient } from '@mcp-ts/sdk/server';
194
+ import { AguiAdapter } from '@mcp-ts/sdk/adapters/agui-adapter';
195
+
196
+ const client = new MultiSessionClient("user_123");
197
+ await client.connect();
198
+
199
+ const adapter = new AguiAdapter(client);
200
+ const tools = await adapter.getTools();
201
+ ```
202
+
203
+ </details>
204
+
205
+ <details>
206
+ <summary>Mastra Adapter</summary>
207
+
208
+ ```typescript
209
+ import { MultiSessionClient } from '@mcp-ts/sdk/server';
210
+ import { MastraAdapter } from '@mcp-ts/sdk/adapters/mastra-adapter';
211
+
212
+ const client = new MultiSessionClient("user_123");
213
+ await client.connect();
214
+
215
+ const tools = await MastraAdapter.getTools(client);
216
+ ```
217
+
218
+ </details>
219
+
220
+ ### 🧩 AG-UI Middleware
221
+
222
+ Execute MCP tools server-side when using remote agents (LangGraph, AutoGen, etc.):
223
+
224
+ <details>
225
+ <summary>View AG-UI (Agent Middleware)</summary>
226
+
227
+ ```typescript
228
+ import { HttpAgent } from "@ag-ui/client";
229
+ import { AguiAdapter } from "@mcp-ts/sdk/adapters/agui-adapter";
230
+ import { createMcpMiddleware } from "@mcp-ts/sdk/adapters/agui-middleware";
231
+
232
+ // Connect to MCP servers
233
+ const { MultiSessionClient } = await import("@mcp-ts/sdk/server");
234
+ const client = new MultiSessionClient("user_123");
235
+ await client.connect();
236
+
237
+ // Create adapter and get tools
238
+ const adapter = new AguiAdapter(client);
239
+ const mcpTools = await adapter.getTools();
240
+
241
+ // Create agent with middleware
242
+ const agent = new HttpAgent({ url: "http://localhost:8000/agent" });
243
+ agent.use(createMcpMiddleware({
244
+ toolPrefix: 'server-',
245
+ tools: mcpTools,
246
+ }));
247
+ ```
248
+
249
+ </details>
250
+
251
+ The middleware intercepts tool calls from remote agents, executes MCP tools server-side, and returns results back to the agent.
252
+
253
+ ### 🛠️ MCP Apps (SEP-1865)
254
+
255
+ Render interactive UIs for your tools using the `useMcpApps` hook.
256
+
257
+ <details>
258
+ <summary>View MCP Apps</summary>
259
+
260
+ ```typescript
261
+ import { useRenderToolCall } from "@copilotkit/react-core";
262
+ import { useMcpApps } from "@mcp-ts/sdk/client/react";
263
+ import { useMcpContext } from "./mcp";
264
+
265
+ export function ToolRenderer() {
266
+ const { mcpClient } = useMcpContext();
267
+ const { getAppMetadata, McpAppRenderer } = useMcpApps(mcpClient);
268
+
269
+ useRenderToolCall({
270
+ name: "*",
271
+ render: ({ name, args, result, status }) => {
272
+ const metadata = getAppMetadata(name);
273
+
274
+ if (!metadata) return null;
275
+
276
+ return (
277
+ <McpAppRenderer
278
+ metadata={metadata}
279
+ input={args}
280
+ result={result}
281
+ status={status}
282
+ sseClient={mcpClient.sseClient}
283
+ />
284
+ );
285
+ },
286
+ });
287
+
288
+ return null;
289
+ }
290
+ ```
291
+
292
+ </details>
293
+
294
+ ## 📚 Documentation
295
+
296
+ Full documentation is available at: **[Docs](https://zonlabs.github.io/mcp-ts/)**
297
+
298
+ ### 🗂️ Topics Covered:
299
+
300
+ - **[Getting Started](https://zonlabs.github.io/mcp-ts/docs/)** - Quick setup and overview
301
+ - **[Installation](https://zonlabs.github.io/mcp-ts/docs/installation)** - Detailed installation guide
302
+ - **[Storage Backends](https://zonlabs.github.io/mcp-ts/docs/storage-backends)** - Redis, File, Memory options
303
+ - **[Next.js Integration](https://zonlabs.github.io/mcp-ts/docs/nextjs)** - Complete Next.js examples
304
+ - **[React Hook Guide](https://zonlabs.github.io/mcp-ts/docs/react-hook)** - Using the useMcp hook
305
+ - **[API Reference](https://zonlabs.github.io/mcp-ts/docs/api-reference)** - Complete API documentation
306
+
307
+ ## ⚙️ Environment Setup
308
+
309
+ The library supports multiple storage backends. You can explicitly select one using `MCP_TS_STORAGE_TYPE` or rely on automatic detection.
310
+
311
+ **Supported Types:** `redis`, `sqlite`, `file`, `memory`.
312
+
313
+ ### 🧷 Configuration Examples
314
+
315
+ 1. **<img src="docs/static/img/storage-backend/redis.svg" width="20" height="20" align="center" /> Redis** (Recommended for production)
316
+ ```bash
317
+ MCP_TS_STORAGE_TYPE=redis
318
+ REDIS_URL=redis://localhost:6379
319
+ ```
320
+
321
+ 2. **<img src="docs/static/img/storage-backend/sqlite.svg" width="20" height="20" align="center" /> SQLite** (Fast & Persistent)
322
+ ```bash
323
+ MCP_TS_STORAGE_TYPE=sqlite
324
+ # Optional path
325
+ MCP_TS_STORAGE_SQLITE_PATH=./sessions.db
326
+ ```
327
+
328
+ 3. **<img src="docs/static/img/storage-backend/filesystem.svg" width="20" height="20" align="center" /> File System** (Great for local dev)
329
+ ```bash
330
+ MCP_TS_STORAGE_TYPE=file
331
+ MCP_TS_STORAGE_FILE=./sessions.json
332
+ ```
333
+
334
+ 4. **<img src="docs/static/img/storage-backend/memory.svg" width="20" height="20" align="center" /> In-Memory** (Default for testing)
335
+ ```bash
336
+ MCP_TS_STORAGE_TYPE=memory
337
+ ```
338
+
339
+ ## 🏗️ Architecture
340
+
341
+ This package uses **Server-Sent Events (SSE)** instead of WebSockets:
342
+
343
+
344
+ ```mermaid
345
+ graph TD
346
+ subgraph Client ["Browser (React)"]
347
+ UI[UI Components]
348
+ Hook[useMcp Hook]
349
+ UI <--> Hook
350
+ end
351
+
352
+ subgraph Server ["Next.js Server (Node.js)"]
353
+ API[API Route /api/mcp]
354
+ SSE[SSE Handler]
355
+ ClientMgr[MCP Client Manager]
356
+
357
+ API <--> ClientMgr
358
+ ClientMgr --> SSE
359
+ end
360
+
361
+ subgraph Infrastructure
362
+ Redis[(Redis Session Store)]
363
+ end
364
+
365
+ subgraph External ["External MCP Servers"]
366
+ TargetServer[Target MCP Server]
367
+ end
368
+
369
+ Hook -- "HTTP POST (RPC)" --> API
370
+ SSE -- "Server-Sent Events" --> Hook
371
+ ClientMgr -- "Persist State" <--> Redis
372
+ ClientMgr -- "MCP Protocol" <--> TargetServer
373
+ ```
374
+
375
+ - **Browser**: React application using the `useMcp` hook for state management.
376
+ - **Next.js Server**: Acts as a bridge, maintaining connections to external MCP servers.
377
+ - **Storage**: Persists session state, OAuth tokens, and connection details (Redis, File, or Memory).
378
+ - **SSE**: Delivers real-time updates (logs, tool list changes) to the client.
379
+
380
+ > [!NOTE]
381
+ > This package (`@mcp-ts/sdk`) provides a unified MCP client with support for adapters and storage backends such as AI SDK, Mastra, LangChain, and Redis.
382
+ > Adapters and storage backends are loaded via **optional peer dependencies** and must be installed independently. This ensures your application only includes the integrations you explicitly choose, keeping bundle size small and avoiding unnecessary dependencies.
383
+ > The SDK includes built-in support for **Memory** and **File** storage, while additional backends (such as Redis) and adapters can be added without impacting users who don’t need them.
384
+
385
+ For more details, refer to the documentation and follow the **installation guide for each adapter or storage backend**.
386
+
387
+ - [AI SDK Installation Guide](https://zonlabs.github.io/mcp-ts/docs/adapters#installation)
388
+ - [Mastra Installation Guide](https://zonlabs.github.io/mcp-ts/docs/adapters#installation)
389
+ - [LangChain Installation Guide](https://zonlabs.github.io/mcp-ts/docs/adapters#installation)
390
+ - [Redis Storage Installation Guide](https://zonlabs.github.io/mcp-ts/docs/storage-backends#-redis-production)
391
+
392
+
393
+ ## 🤝 Contributing
394
+
395
+ Contributions are welcome! Please read [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines on how to contribute.
396
+
397
+
398
+ <br />
399
+
400
+ <p align="center">
401
+ <em> Thanks for visiting ✨ @mcp-ts!</em><br><br>
402
+ <img src="https://visitor-badge.laobi.icu/badge?page_id=zonlabs.mcp-ts&style=for-the-badge&color=00d4ff" alt="Views">
403
+ </p>
404
+
405
+