@karashiiro/mcp 0.2.0 → 0.3.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 +64 -5
- package/dist/http.d.ts +45 -12
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +17 -24
- package/dist/http.js.map +1 -1
- package/dist/stdio.d.ts +6 -4
- package/dist/stdio.d.ts.map +1 -1
- package/dist/stdio.js +3 -1
- package/dist/stdio.js.map +1 -1
- package/dist/types.d.ts +16 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -96,6 +96,41 @@ const handle = await serveHttp(createServer, {
|
|
|
96
96
|
|
|
97
97
|
In stateful mode, `createServer` is called once per client session, allowing each client to have isolated state.
|
|
98
98
|
|
|
99
|
+
#### Session-Aware Factories
|
|
100
|
+
|
|
101
|
+
In stateful mode, the factory function receives the session ID as a parameter, enabling session-specific initialization:
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
const handle = await serveHttp(
|
|
105
|
+
(sessionId) => {
|
|
106
|
+
console.log(`Creating server for session: ${sessionId}`);
|
|
107
|
+
return createServer();
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
port: 8080,
|
|
111
|
+
sessions: {},
|
|
112
|
+
},
|
|
113
|
+
);
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
#### Async Factories
|
|
117
|
+
|
|
118
|
+
The factory function can be async, which is useful for initialization that requires async operations:
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
const handle = await serveHttp(
|
|
122
|
+
async (sessionId) => {
|
|
123
|
+
// Perform async initialization (e.g., connect to database, load config)
|
|
124
|
+
await initializeResources(sessionId);
|
|
125
|
+
return createServer();
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
port: 8080,
|
|
129
|
+
sessions: {},
|
|
130
|
+
},
|
|
131
|
+
);
|
|
132
|
+
```
|
|
133
|
+
|
|
99
134
|
#### Custom Session IDs
|
|
100
135
|
|
|
101
136
|
```ts
|
|
@@ -137,22 +172,46 @@ If you only need stdio transport, import from `@karashiiro/mcp/stdio` to avoid b
|
|
|
137
172
|
|
|
138
173
|
## API Reference
|
|
139
174
|
|
|
175
|
+
### Factory Types
|
|
176
|
+
|
|
177
|
+
The library provides two factory types for type-safe server creation:
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
// For stateless mode (serveStdio and serveHttp without sessions)
|
|
181
|
+
type StatelessServerFactory = () => McpServer | Promise<McpServer>;
|
|
182
|
+
|
|
183
|
+
// For stateful mode (serveHttp with sessions)
|
|
184
|
+
type StatefulServerFactory = (sessionId: string) => McpServer | Promise<McpServer>;
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Both factory types support async initialization by returning a `Promise<McpServer>`.
|
|
188
|
+
|
|
140
189
|
### `serveStdio(serverFactory)`
|
|
141
190
|
|
|
142
191
|
Serves an MCP server over stdin/stdout.
|
|
143
192
|
|
|
144
|
-
- `serverFactory:
|
|
193
|
+
- `serverFactory: StatelessServerFactory` - Factory function that creates a single server instance
|
|
194
|
+
- Returns: `Promise<ServerHandle>`
|
|
195
|
+
|
|
196
|
+
### `serveHttp(serverFactory, options?)` (stateless)
|
|
197
|
+
|
|
198
|
+
Serves an MCP server over HTTP in stateless mode.
|
|
199
|
+
|
|
200
|
+
- `serverFactory: StatelessServerFactory` - Factory function called once, creates a shared server
|
|
201
|
+
- `options.port` - Port to listen on (default: `8080`)
|
|
202
|
+
- `options.host` - Host to bind to (default: `"127.0.0.1"`)
|
|
203
|
+
- `options.endpoint` - MCP endpoint path (default: `"/mcp"`)
|
|
145
204
|
- Returns: `Promise<ServerHandle>`
|
|
146
205
|
|
|
147
|
-
### `serveHttp(serverFactory, options
|
|
206
|
+
### `serveHttp(serverFactory, options)` (stateful)
|
|
148
207
|
|
|
149
|
-
Serves an MCP server over HTTP.
|
|
208
|
+
Serves an MCP server over HTTP in stateful mode with per-client sessions.
|
|
150
209
|
|
|
151
|
-
- `serverFactory:
|
|
210
|
+
- `serverFactory: StatefulServerFactory` - Factory function called per session with the session ID
|
|
152
211
|
- `options.port` - Port to listen on (default: `8080`)
|
|
153
212
|
- `options.host` - Host to bind to (default: `"127.0.0.1"`)
|
|
154
213
|
- `options.endpoint` - MCP endpoint path (default: `"/mcp"`)
|
|
155
|
-
- `options.sessions` -
|
|
214
|
+
- `options.sessions` - **Required** to enable stateful mode
|
|
156
215
|
- `options.sessions.sessionIdGenerator` - Custom session ID generator function
|
|
157
216
|
- `options.sessions.legacySse` - Enable legacy SSE transport endpoints
|
|
158
217
|
- Returns: `Promise<ServerHandle>`
|
package/dist/http.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
export type {
|
|
1
|
+
import type { ServerHandle, StatelessServerFactory, StatefulServerFactory } from "./types.js";
|
|
2
|
+
export type { ServerHandle, StatelessServerFactory, StatefulServerFactory, } from "./types.js";
|
|
3
|
+
export type { ServerFactory } from "./types.js";
|
|
4
4
|
/**
|
|
5
5
|
* Options for legacy SSE transport compatibility.
|
|
6
6
|
* @deprecated SSE transport is deprecated in favor of Streamable HTTP.
|
|
@@ -20,21 +20,54 @@ export interface HttpServerSessionOptions {
|
|
|
20
20
|
*/
|
|
21
21
|
legacySse?: LegacySseOptions;
|
|
22
22
|
}
|
|
23
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Base HTTP server options without session configuration.
|
|
25
|
+
*/
|
|
26
|
+
export interface HttpServerOptionsBase {
|
|
24
27
|
port: number;
|
|
25
28
|
host: string;
|
|
26
29
|
endpoint: string;
|
|
27
|
-
sessions?: HttpServerSessionOptions | undefined;
|
|
28
30
|
}
|
|
29
31
|
/**
|
|
30
|
-
*
|
|
32
|
+
* HTTP server options for stateless mode (no sessions).
|
|
33
|
+
*/
|
|
34
|
+
export interface HttpServerStatelessOptions extends HttpServerOptionsBase {
|
|
35
|
+
sessions?: undefined;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* HTTP server options for stateful mode (with sessions).
|
|
39
|
+
*/
|
|
40
|
+
export interface HttpServerStatefulOptions extends HttpServerOptionsBase {
|
|
41
|
+
sessions: HttpServerSessionOptions;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Combined HTTP server options type.
|
|
45
|
+
*/
|
|
46
|
+
export type HttpServerOptions = HttpServerStatelessOptions | HttpServerStatefulOptions;
|
|
47
|
+
/**
|
|
48
|
+
* Serve an MCP server over HTTP in stateless mode.
|
|
49
|
+
*
|
|
50
|
+
* In stateless mode, the factory is called once and all clients share the same server instance.
|
|
51
|
+
* The factory receives no parameters.
|
|
52
|
+
*
|
|
53
|
+
* @param serverFactory - Factory function that creates a single shared McpServer instance.
|
|
54
|
+
* @param options - Server configuration options (without sessions).
|
|
55
|
+
* @returns A handle to control the server lifecycle.
|
|
56
|
+
*/
|
|
57
|
+
export declare function serveHttp(serverFactory: StatelessServerFactory, options?: Partial<Omit<HttpServerOptionsBase, "sessions">> & {
|
|
58
|
+
sessions?: undefined;
|
|
59
|
+
}): Promise<ServerHandle>;
|
|
60
|
+
/**
|
|
61
|
+
* Serve an MCP server over HTTP in stateful mode with per-client sessions.
|
|
62
|
+
*
|
|
63
|
+
* In stateful mode, the factory is called once per client session, receiving the session ID.
|
|
64
|
+
* This allows each client to have isolated state.
|
|
31
65
|
*
|
|
32
|
-
* @param serverFactory - Factory function that creates McpServer instances.
|
|
33
|
-
*
|
|
34
|
-
* @param options - Server configuration options.
|
|
35
|
-
* If `sessions` is provided, runs in stateful mode with per-session servers.
|
|
36
|
-
* If `sessions` is undefined, runs in stateless mode with a single server.
|
|
66
|
+
* @param serverFactory - Factory function that creates McpServer instances per session.
|
|
67
|
+
* @param options - Server configuration options with sessions enabled.
|
|
37
68
|
* @returns A handle to control the server lifecycle.
|
|
38
69
|
*/
|
|
39
|
-
export declare function serveHttp(serverFactory:
|
|
70
|
+
export declare function serveHttp(serverFactory: StatefulServerFactory, options: Partial<HttpServerOptionsBase> & {
|
|
71
|
+
sessions: HttpServerSessionOptions;
|
|
72
|
+
}): Promise<ServerHandle>;
|
|
40
73
|
//# sourceMappingURL=http.d.ts.map
|
package/dist/http.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EACV,YAAY,EACZ,sBAAsB,EACtB,qBAAqB,EACtB,MAAM,YAAY,CAAC;AAEpB,YAAY,EACV,YAAY,EACZ,sBAAsB,EACtB,qBAAqB,GACtB,MAAM,YAAY,CAAC;AAGpB,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,mDAAmD;IACnD,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,sDAAsD;IACtD,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,MAAM,WAAW,wBAAwB;IACvC,kBAAkB,CAAC,EAAE,MAAM,MAAM,CAAC;IAClC;;;;OAIG;IACH,SAAS,CAAC,EAAE,gBAAgB,CAAC;CAC9B;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,0BAA2B,SAAQ,qBAAqB;IACvE,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,yBAA0B,SAAQ,qBAAqB;IACtE,QAAQ,EAAE,wBAAwB,CAAC;CACpC;AAED;;GAEG;AACH,MAAM,MAAM,iBAAiB,GACzB,0BAA0B,GAC1B,yBAAyB,CAAC;AAqD9B;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CACvB,aAAa,EAAE,sBAAsB,EACrC,OAAO,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,qBAAqB,EAAE,UAAU,CAAC,CAAC,GAAG;IAC3D,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB,GACA,OAAO,CAAC,YAAY,CAAC,CAAC;AAEzB;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CACvB,aAAa,EAAE,qBAAqB,EACpC,OAAO,EAAE,OAAO,CAAC,qBAAqB,CAAC,GAAG;IACxC,QAAQ,EAAE,wBAAwB,CAAC;CACpC,GACA,OAAO,CAAC,YAAY,CAAC,CAAC"}
|
package/dist/http.js
CHANGED
|
@@ -28,16 +28,7 @@ function createHandle(server) {
|
|
|
28
28
|
}),
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
|
-
|
|
32
|
-
* Serve an MCP server over HTTP.
|
|
33
|
-
*
|
|
34
|
-
* @param serverFactory - Factory function that creates McpServer instances.
|
|
35
|
-
* In stateless mode, called once. In stateful mode, called per session.
|
|
36
|
-
* @param options - Server configuration options.
|
|
37
|
-
* If `sessions` is provided, runs in stateful mode with per-session servers.
|
|
38
|
-
* If `sessions` is undefined, runs in stateless mode with a single server.
|
|
39
|
-
* @returns A handle to control the server lifecycle.
|
|
40
|
-
*/
|
|
31
|
+
// Implementation signature (accepts both)
|
|
41
32
|
export async function serveHttp(serverFactory, options = {}) {
|
|
42
33
|
const mergedOptions = {
|
|
43
34
|
...defaultOptions,
|
|
@@ -54,8 +45,8 @@ export async function serveHttp(serverFactory, options = {}) {
|
|
|
54
45
|
* Stateless mode: single server instance, single transport, no session tracking.
|
|
55
46
|
*/
|
|
56
47
|
async function serveHttpStateless(serverFactory, options) {
|
|
57
|
-
// Call factory ONCE to get the single server instance
|
|
58
|
-
const server = serverFactory();
|
|
48
|
+
// Call factory ONCE to get the single server instance (no sessionId in stateless mode)
|
|
49
|
+
const server = await serverFactory();
|
|
59
50
|
// Create the transport (no session ID generator = stateless)
|
|
60
51
|
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
61
52
|
sessionIdGenerator: undefined,
|
|
@@ -78,7 +69,7 @@ async function serveHttpStateless(serverFactory, options) {
|
|
|
78
69
|
*/
|
|
79
70
|
function serveHttpStateful(serverFactory, options) {
|
|
80
71
|
const sessions = new Map();
|
|
81
|
-
const sessionIdGenerator = options.sessions
|
|
72
|
+
const sessionIdGenerator = options.sessions.sessionIdGenerator ?? uuidv4;
|
|
82
73
|
// Legacy SSE options
|
|
83
74
|
const legacySse = options.sessions?.legacySse;
|
|
84
75
|
const sseEndpoint = legacySse?.sseEndpoint ?? "/sse";
|
|
@@ -106,13 +97,17 @@ function serveHttpStateful(serverFactory, options) {
|
|
|
106
97
|
});
|
|
107
98
|
// New session (initialize request without session ID)
|
|
108
99
|
if (!sessionId && body && isInitializeRequest(body)) {
|
|
100
|
+
// Generate session ID upfront so we can pass it to the factory
|
|
101
|
+
const sid = sessionIdGenerator();
|
|
102
|
+
// Create server with session ID (supports async factories)
|
|
103
|
+
const server = await serverFactory(sid);
|
|
109
104
|
const eventStore = new InMemoryEventStore();
|
|
110
105
|
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
111
|
-
|
|
106
|
+
// Use our pre-generated session ID
|
|
107
|
+
sessionIdGenerator: () => sid,
|
|
112
108
|
eventStore,
|
|
113
|
-
onsessioninitialized: (
|
|
114
|
-
//
|
|
115
|
-
const server = serverFactory();
|
|
109
|
+
onsessioninitialized: () => {
|
|
110
|
+
// Server already created above, just store the session state
|
|
116
111
|
const session = {
|
|
117
112
|
type: "streamable-http",
|
|
118
113
|
transport,
|
|
@@ -120,13 +115,12 @@ function serveHttpStateful(serverFactory, options) {
|
|
|
120
115
|
eventStore,
|
|
121
116
|
};
|
|
122
117
|
sessions.set(sid, session);
|
|
123
|
-
server.connect(transport);
|
|
124
118
|
},
|
|
125
119
|
});
|
|
120
|
+
// Connect server to transport before handling the request
|
|
121
|
+
await server.connect(transport);
|
|
126
122
|
transport.onclose = () => {
|
|
127
|
-
|
|
128
|
-
sessions.delete(transport.sessionId);
|
|
129
|
-
}
|
|
123
|
+
sessions.delete(sid);
|
|
130
124
|
};
|
|
131
125
|
return transport.handleRequest(recreateRequest());
|
|
132
126
|
}
|
|
@@ -159,8 +153,8 @@ function serveHttpStateful(serverFactory, options) {
|
|
|
159
153
|
const { outgoing } = c.env;
|
|
160
154
|
// Create SSE transport with messages endpoint
|
|
161
155
|
const transport = new SSEServerTransport(messagesEndpoint, outgoing);
|
|
162
|
-
// Create
|
|
163
|
-
const server = serverFactory();
|
|
156
|
+
// Create server with session ID (supports async factories)
|
|
157
|
+
const server = await serverFactory(transport.sessionId);
|
|
164
158
|
// Store session
|
|
165
159
|
const session = {
|
|
166
160
|
type: "sse",
|
|
@@ -219,7 +213,6 @@ function serveHttpStateful(serverFactory, options) {
|
|
|
219
213
|
});
|
|
220
214
|
return createHandle(httpServer);
|
|
221
215
|
}
|
|
222
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
223
216
|
function addCors(app) {
|
|
224
217
|
// Enable CORS for all origins
|
|
225
218
|
app.use("*", cors({
|
package/dist/http.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.js","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAmB,MAAM,mBAAmB,CAAC;AAE3D,OAAO,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AAEzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAC;AAC7E,OAAO,EAAE,wCAAwC,EAAE,MAAM,+DAA+D,CAAC;AACzH,OAAO,EAAE,mBAAmB,EAAE,MAAM,oCAAoC,CAAC;AACzE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,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AI,EAAE,OAAO,CAAC,IAAI;QAClB,QAAQ,EAAE,OAAO,CAAC,IAAI;KACvB,CAAC,CAAC;IAEH,OAAO,YAAY,CAAC,UAAU,CAAC,CAAC;AAAA,CACjC;AAED,SAAS,OAAO,CAAC,GAAqC,EAAQ;IAC5D,8BAA8B;IAC9B,GAAG,CAAC,GAAG,CACL,GAAG,EACH,IAAI,CAAC;QACH,MAAM,EAAE,GAAG;QACX,YAAY,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,CAAC;QAClD,YAAY,EAAE;YACZ,cAAc;YACd,gBAAgB;YAChB,eAAe;YACf,sBAAsB;SACvB;QACD,aAAa,EAAE,CAAC,gBAAgB,EAAE,sBAAsB,CAAC;KAC1D,CAAC,CACH,CAAC;AAAA,CACH"}
|
package/dist/stdio.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
export type {
|
|
1
|
+
import type { ServerHandle, StatelessServerFactory } from "./types.js";
|
|
2
|
+
export type { ServerHandle, StatelessServerFactory } from "./types.js";
|
|
3
|
+
export type { ServerFactory } from "./types.js";
|
|
4
4
|
/**
|
|
5
5
|
* Serve an MCP server over stdio (stdin/stdout).
|
|
6
6
|
*
|
|
7
7
|
* @param serverFactory - Factory function that creates an McpServer instance.
|
|
8
|
+
* Called once with no parameters (stdio is always single-session).
|
|
9
|
+
* Can return a Promise for async initialization.
|
|
8
10
|
* @returns A handle to control the server lifecycle.
|
|
9
11
|
*/
|
|
10
|
-
export declare function serveStdio(serverFactory:
|
|
12
|
+
export declare function serveStdio(serverFactory: StatelessServerFactory): Promise<ServerHandle>;
|
|
11
13
|
//# sourceMappingURL=stdio.d.ts.map
|
package/dist/stdio.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stdio.d.ts","sourceRoot":"","sources":["../src/stdio.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stdio.d.ts","sourceRoot":"","sources":["../src/stdio.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAEvE,YAAY,EAAE,YAAY,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAGvE,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD;;;;;;;GAOG;AACH,wBAAsB,UAAU,CAC9B,aAAa,EAAE,sBAAsB,GACpC,OAAO,CAAC,YAAY,CAAC,CA4BvB"}
|
package/dist/stdio.js
CHANGED
|
@@ -3,10 +3,12 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
|
|
|
3
3
|
* Serve an MCP server over stdio (stdin/stdout).
|
|
4
4
|
*
|
|
5
5
|
* @param serverFactory - Factory function that creates an McpServer instance.
|
|
6
|
+
* Called once with no parameters (stdio is always single-session).
|
|
7
|
+
* Can return a Promise for async initialization.
|
|
6
8
|
* @returns A handle to control the server lifecycle.
|
|
7
9
|
*/
|
|
8
10
|
export async function serveStdio(serverFactory) {
|
|
9
|
-
const server = serverFactory();
|
|
11
|
+
const server = await serverFactory();
|
|
10
12
|
const transport = new StdioServerTransport();
|
|
11
13
|
// Set up the closed promise before connecting
|
|
12
14
|
let resolveClose;
|
package/dist/stdio.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stdio.js","sourceRoot":"","sources":["../src/stdio.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stdio.js","sourceRoot":"","sources":["../src/stdio.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AAQjF;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,aAAqC,EACd;IACvB,MAAM,MAAM,GAAG,MAAM,aAAa,EAAE,CAAC;IAErC,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;IAE7C,8CAA8C;IAC9C,IAAI,YAAwB,CAAC;IAC7B,MAAM,aAAa,GAAG,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE,CAAC;QACnD,YAAY,GAAG,OAAO,CAAC;IAAA,CACxB,CAAC,CAAC;IAEH,SAAS,CAAC,OAAO,GAAG,GAAG,EAAE,CAAC;QACxB,YAAY,EAAE,CAAC;IAAA,CAChB,CAAC;IAEF,4DAA4D;IAC5D,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAEhC,IAAI,YAAuC,CAAC;IAE5C,OAAO;QACL,KAAK,EAAE,GAAG,EAAE,CAAC;YACX,IAAI,CAAC,YAAY,EAAE,CAAC;gBAClB,YAAY,GAAG,SAAS,CAAC,KAAK,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,aAAa,CAAC,CAAC;YAC7D,CAAC;YACD,OAAO,YAAY,CAAC;QAAA,CACrB;KACF,CAAC;AAAA,CACH"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
1
2
|
/**
|
|
2
3
|
* Handle returned by serve functions for controlling the server lifecycle.
|
|
3
4
|
*/
|
|
@@ -5,4 +6,19 @@ export interface ServerHandle {
|
|
|
5
6
|
/** Close the server and stop accepting new connections. */
|
|
6
7
|
close: () => Promise<void>;
|
|
7
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Factory function for stateless mode (no session ID).
|
|
11
|
+
* Used by serveStdio and serveHttp without sessions.
|
|
12
|
+
*/
|
|
13
|
+
export type StatelessServerFactory = () => McpServer | Promise<McpServer>;
|
|
14
|
+
/**
|
|
15
|
+
* Factory function for stateful mode (receives session ID).
|
|
16
|
+
* Used by serveHttp with sessions enabled.
|
|
17
|
+
*/
|
|
18
|
+
export type StatefulServerFactory = (sessionId: string) => McpServer | Promise<McpServer>;
|
|
19
|
+
/**
|
|
20
|
+
* Union type for all server factory signatures.
|
|
21
|
+
* @deprecated Prefer using StatelessServerFactory or StatefulServerFactory directly.
|
|
22
|
+
*/
|
|
23
|
+
export type ServerFactory = (sessionId?: string) => McpServer | Promise<McpServer>;
|
|
8
24
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,2DAA2D;IAC3D,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5B"}
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAEzE;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,2DAA2D;IAC3D,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5B;AAED;;;GAGG;AACH,MAAM,MAAM,sBAAsB,GAAG,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;AAE1E;;;GAGG;AACH,MAAM,MAAM,qBAAqB,GAAG,CAClC,SAAS,EAAE,MAAM,KACd,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;AAEpC;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG,CAC1B,SAAS,CAAC,EAAE,MAAM,KACf,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC"}
|