@contractspec/bundle.library 2.4.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +14 -14
- package/CHANGELOG.md +51 -0
- package/dist/application/index.js +138 -49
- package/dist/application/mcp/cliMcp.d.ts +3 -56
- package/dist/application/mcp/cliMcp.js +138 -49
- package/dist/application/mcp/common.d.ts +5 -57
- package/dist/application/mcp/common.js +138 -49
- package/dist/application/mcp/docsMcp.d.ts +3 -56
- package/dist/application/mcp/docsMcp.js +138 -49
- package/dist/application/mcp/index.js +138 -49
- package/dist/application/mcp/internalMcp.d.ts +3 -56
- package/dist/application/mcp/internalMcp.js +138 -49
- package/dist/node/application/index.js +138 -49
- package/dist/node/application/mcp/cliMcp.js +138 -49
- package/dist/node/application/mcp/common.js +138 -49
- package/dist/node/application/mcp/docsMcp.js +138 -49
- package/dist/node/application/mcp/index.js +138 -49
- package/dist/node/application/mcp/internalMcp.js +138 -49
- package/package.json +17 -17
- package/src/application/mcp/common.ts +180 -87
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { OperationSpecRegistry, PromptRegistry, ResourceRegistry } from '@contractspec/lib.contracts-spec';
|
|
2
2
|
import type { PresentationSpec } from '@contractspec/lib.contracts-spec/presentations';
|
|
3
|
+
import { Elysia } from 'elysia';
|
|
3
4
|
import { Logger } from '@contractspec/lib.logger';
|
|
4
5
|
interface McpHttpHandlerConfig {
|
|
5
6
|
path: string;
|
|
@@ -10,11 +11,9 @@ interface McpHttpHandlerConfig {
|
|
|
10
11
|
presentations?: PresentationSpec[];
|
|
11
12
|
logger: Logger;
|
|
12
13
|
}
|
|
13
|
-
export declare function createMcpElysiaHandler({ logger, path, serverName, ops, resources, prompts, presentations, }: McpHttpHandlerConfig):
|
|
14
|
+
export declare function createMcpElysiaHandler({ logger, path, serverName, ops, resources, prompts, presentations, }: McpHttpHandlerConfig): Elysia<"", {
|
|
14
15
|
decorator: {};
|
|
15
|
-
store: {
|
|
16
|
-
authInfo: AuthInfo | undefined;
|
|
17
|
-
};
|
|
16
|
+
store: {};
|
|
18
17
|
derive: {};
|
|
19
18
|
resolve: {};
|
|
20
19
|
}, {
|
|
@@ -28,39 +27,6 @@ export declare function createMcpElysiaHandler({ logger, path, serverName, ops,
|
|
|
28
27
|
parser: {};
|
|
29
28
|
response: {};
|
|
30
29
|
}, {
|
|
31
|
-
[x: string]: {
|
|
32
|
-
"*": {
|
|
33
|
-
[x: string]: {
|
|
34
|
-
body: unknown;
|
|
35
|
-
params: {
|
|
36
|
-
"*": string;
|
|
37
|
-
} & {};
|
|
38
|
-
query: unknown;
|
|
39
|
-
headers: unknown;
|
|
40
|
-
response: {
|
|
41
|
-
200: {} | {
|
|
42
|
-
jsonrpc: string;
|
|
43
|
-
error: {
|
|
44
|
-
code: import("@modelcontextprotocol/sdk/types.js").ErrorCode;
|
|
45
|
-
message: string;
|
|
46
|
-
data: string;
|
|
47
|
-
};
|
|
48
|
-
id: null;
|
|
49
|
-
};
|
|
50
|
-
422: {
|
|
51
|
-
type: "validation";
|
|
52
|
-
on: string;
|
|
53
|
-
summary?: string;
|
|
54
|
-
message?: string;
|
|
55
|
-
found?: unknown;
|
|
56
|
-
property?: string;
|
|
57
|
-
expected?: string;
|
|
58
|
-
};
|
|
59
|
-
};
|
|
60
|
-
};
|
|
61
|
-
};
|
|
62
|
-
};
|
|
63
|
-
} & {
|
|
64
30
|
[x: string]: {
|
|
65
31
|
[x: string]: {
|
|
66
32
|
body: unknown;
|
|
@@ -68,15 +34,7 @@ export declare function createMcpElysiaHandler({ logger, path, serverName, ops,
|
|
|
68
34
|
query: unknown;
|
|
69
35
|
headers: unknown;
|
|
70
36
|
response: {
|
|
71
|
-
200:
|
|
72
|
-
jsonrpc: string;
|
|
73
|
-
error: {
|
|
74
|
-
code: import("@modelcontextprotocol/sdk/types.js").ErrorCode;
|
|
75
|
-
message: string;
|
|
76
|
-
data: string;
|
|
77
|
-
};
|
|
78
|
-
id: null;
|
|
79
|
-
};
|
|
37
|
+
200: Response;
|
|
80
38
|
};
|
|
81
39
|
};
|
|
82
40
|
};
|
|
@@ -91,16 +49,6 @@ export declare function createMcpElysiaHandler({ logger, path, serverName, ops,
|
|
|
91
49
|
resolve: {};
|
|
92
50
|
schema: {};
|
|
93
51
|
standaloneSchema: {};
|
|
94
|
-
response: {
|
|
95
|
-
200: {} | {
|
|
96
|
-
jsonrpc: string;
|
|
97
|
-
error: {
|
|
98
|
-
code: import("@modelcontextprotocol/sdk/types.js").ErrorCode;
|
|
99
|
-
message: string;
|
|
100
|
-
data: string;
|
|
101
|
-
};
|
|
102
|
-
id: null;
|
|
103
|
-
};
|
|
104
|
-
};
|
|
52
|
+
response: {};
|
|
105
53
|
}>;
|
|
106
54
|
export {};
|
|
@@ -2,72 +2,161 @@
|
|
|
2
2
|
// src/application/mcp/common.ts
|
|
3
3
|
import { PresentationRegistry } from "@contractspec/lib.contracts-spec/presentations";
|
|
4
4
|
import { createMcpServer } from "@contractspec/lib.contracts-runtime-server-mcp/provider-mcp";
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return {
|
|
10
|
-
log: (...args) => {
|
|
11
|
-
if (!isDebug)
|
|
12
|
-
return;
|
|
13
|
-
logger.info(toMessage(args));
|
|
14
|
-
},
|
|
15
|
-
info: (...args) => {
|
|
16
|
-
if (!isDebug)
|
|
17
|
-
return;
|
|
18
|
-
logger.info(toMessage(args));
|
|
19
|
-
},
|
|
20
|
-
warn: (...args) => {
|
|
21
|
-
logger.warn(toMessage(args));
|
|
22
|
-
},
|
|
23
|
-
error: (...args) => {
|
|
24
|
-
logger.error(toMessage(args));
|
|
25
|
-
},
|
|
26
|
-
debug: (...args) => {
|
|
27
|
-
if (!isDebug)
|
|
28
|
-
return;
|
|
29
|
-
logger.debug(toMessage(args));
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
}
|
|
5
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
6
|
+
import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js";
|
|
7
|
+
import { Elysia } from "elysia";
|
|
8
|
+
import { randomUUID } from "crypto";
|
|
33
9
|
var baseCtx = {
|
|
34
10
|
actor: "anonymous",
|
|
35
11
|
decide: async () => ({ effect: "allow" })
|
|
36
12
|
};
|
|
37
|
-
function
|
|
13
|
+
function createJsonRpcErrorResponse(status, code, message, data) {
|
|
14
|
+
return new Response(JSON.stringify({
|
|
15
|
+
jsonrpc: "2.0",
|
|
16
|
+
error: {
|
|
17
|
+
code,
|
|
18
|
+
message,
|
|
19
|
+
...data ? { data } : {}
|
|
20
|
+
},
|
|
21
|
+
id: null
|
|
22
|
+
}), {
|
|
23
|
+
status,
|
|
24
|
+
headers: {
|
|
25
|
+
"content-type": "application/json"
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function createSessionState({
|
|
38
30
|
logger,
|
|
39
|
-
path,
|
|
40
31
|
serverName,
|
|
41
32
|
ops,
|
|
42
33
|
resources,
|
|
43
34
|
prompts,
|
|
44
|
-
presentations
|
|
35
|
+
presentations,
|
|
36
|
+
stateful
|
|
45
37
|
}) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
serverInfo: {
|
|
51
|
-
name: serverName,
|
|
52
|
-
version: "1.0.0"
|
|
53
|
-
},
|
|
54
|
-
stateless: process.env.CONTRACTSPEC_MCP_STATEFUL !== "1",
|
|
55
|
-
enableJsonResponse: true,
|
|
38
|
+
const server = new McpServer({
|
|
39
|
+
name: serverName,
|
|
40
|
+
version: "1.0.0"
|
|
41
|
+
}, {
|
|
56
42
|
capabilities: {
|
|
57
43
|
tools: {},
|
|
58
44
|
resources: {},
|
|
59
45
|
prompts: {},
|
|
60
46
|
logging: {}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
logger.info("Setting up MCP server...");
|
|
50
|
+
createMcpServer(server, ops, resources, prompts, {
|
|
51
|
+
logger,
|
|
52
|
+
toolCtx: () => baseCtx,
|
|
53
|
+
promptCtx: () => ({ locale: "en" }),
|
|
54
|
+
resourceCtx: () => ({ locale: "en" }),
|
|
55
|
+
presentations: new PresentationRegistry(presentations)
|
|
56
|
+
});
|
|
57
|
+
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
58
|
+
sessionIdGenerator: stateful ? () => randomUUID() : undefined,
|
|
59
|
+
enableJsonResponse: true
|
|
60
|
+
});
|
|
61
|
+
return server.connect(transport).then(() => ({ server, transport }));
|
|
62
|
+
}
|
|
63
|
+
async function closeSessionState(state) {
|
|
64
|
+
await Promise.allSettled([state.transport.close(), state.server.close()]);
|
|
65
|
+
}
|
|
66
|
+
function toErrorMessage(error) {
|
|
67
|
+
return error instanceof Error ? error.stack ?? error.message : String(error);
|
|
68
|
+
}
|
|
69
|
+
function createMcpElysiaHandler({
|
|
70
|
+
logger,
|
|
71
|
+
path,
|
|
72
|
+
serverName,
|
|
73
|
+
ops,
|
|
74
|
+
resources,
|
|
75
|
+
prompts,
|
|
76
|
+
presentations
|
|
77
|
+
}) {
|
|
78
|
+
logger.info("Setting up MCP handler...");
|
|
79
|
+
const isStateful = process.env.CONTRACTSPEC_MCP_STATEFUL === "1";
|
|
80
|
+
const sessions = new Map;
|
|
81
|
+
async function handleStateless(request) {
|
|
82
|
+
const state = await createSessionState({
|
|
83
|
+
logger,
|
|
84
|
+
path,
|
|
85
|
+
serverName,
|
|
86
|
+
ops,
|
|
87
|
+
resources,
|
|
88
|
+
prompts,
|
|
89
|
+
presentations,
|
|
90
|
+
stateful: false
|
|
91
|
+
});
|
|
92
|
+
try {
|
|
93
|
+
return await state.transport.handleRequest(request);
|
|
94
|
+
} finally {
|
|
95
|
+
await closeSessionState(state);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async function closeSession(sessionId) {
|
|
99
|
+
const state = sessions.get(sessionId);
|
|
100
|
+
if (!state)
|
|
101
|
+
return;
|
|
102
|
+
sessions.delete(sessionId);
|
|
103
|
+
await closeSessionState(state);
|
|
104
|
+
}
|
|
105
|
+
async function handleStateful(request) {
|
|
106
|
+
const requestedSessionId = request.headers.get("mcp-session-id");
|
|
107
|
+
let state;
|
|
108
|
+
let createdState = false;
|
|
109
|
+
if (requestedSessionId) {
|
|
110
|
+
const existing = sessions.get(requestedSessionId);
|
|
111
|
+
if (!existing) {
|
|
112
|
+
return createJsonRpcErrorResponse(404, -32001, "Session not found");
|
|
113
|
+
}
|
|
114
|
+
state = existing;
|
|
115
|
+
} else {
|
|
116
|
+
state = await createSessionState({
|
|
65
117
|
logger,
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
118
|
+
path,
|
|
119
|
+
serverName,
|
|
120
|
+
ops,
|
|
121
|
+
resources,
|
|
122
|
+
prompts,
|
|
123
|
+
presentations,
|
|
124
|
+
stateful: true
|
|
125
|
+
});
|
|
126
|
+
createdState = true;
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
const response = await state.transport.handleRequest(request);
|
|
130
|
+
const activeSessionId = state.transport.sessionId;
|
|
131
|
+
if (activeSessionId && !sessions.has(activeSessionId)) {
|
|
132
|
+
sessions.set(activeSessionId, state);
|
|
133
|
+
}
|
|
134
|
+
if (request.method === "DELETE" && activeSessionId) {
|
|
135
|
+
await closeSession(activeSessionId);
|
|
136
|
+
} else if (!activeSessionId && createdState) {
|
|
137
|
+
await closeSessionState(state);
|
|
138
|
+
}
|
|
139
|
+
return response;
|
|
140
|
+
} catch (error) {
|
|
141
|
+
if (createdState) {
|
|
142
|
+
await closeSessionState(state);
|
|
143
|
+
}
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return new Elysia({ name: `mcp-${serverName}` }).all(path, async ({ request }) => {
|
|
148
|
+
try {
|
|
149
|
+
if (isStateful) {
|
|
150
|
+
return await handleStateful(request);
|
|
151
|
+
}
|
|
152
|
+
return await handleStateless(request);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
logger.error("Error handling MCP request", {
|
|
155
|
+
path,
|
|
156
|
+
method: request.method,
|
|
157
|
+
error: toErrorMessage(error)
|
|
70
158
|
});
|
|
159
|
+
return createJsonRpcErrorResponse(500, -32000, "Internal error");
|
|
71
160
|
}
|
|
72
161
|
});
|
|
73
162
|
}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
export declare function createDocsMcpHandler(path?: string): import("elysia").default<"", {
|
|
2
2
|
decorator: {};
|
|
3
|
-
store: {
|
|
4
|
-
authInfo: AuthInfo | undefined;
|
|
5
|
-
};
|
|
3
|
+
store: {};
|
|
6
4
|
derive: {};
|
|
7
5
|
resolve: {};
|
|
8
6
|
}, {
|
|
@@ -16,39 +14,6 @@ export declare function createDocsMcpHandler(path?: string): import("elysia").de
|
|
|
16
14
|
parser: {};
|
|
17
15
|
response: {};
|
|
18
16
|
}, {
|
|
19
|
-
[x: string]: {
|
|
20
|
-
"*": {
|
|
21
|
-
[x: string]: {
|
|
22
|
-
body: unknown;
|
|
23
|
-
params: {
|
|
24
|
-
"*": string;
|
|
25
|
-
} & {};
|
|
26
|
-
query: unknown;
|
|
27
|
-
headers: unknown;
|
|
28
|
-
response: {
|
|
29
|
-
200: {} | {
|
|
30
|
-
jsonrpc: string;
|
|
31
|
-
error: {
|
|
32
|
-
code: import("@modelcontextprotocol/sdk/types.js").ErrorCode;
|
|
33
|
-
message: string;
|
|
34
|
-
data: string;
|
|
35
|
-
};
|
|
36
|
-
id: null;
|
|
37
|
-
};
|
|
38
|
-
422: {
|
|
39
|
-
type: "validation";
|
|
40
|
-
on: string;
|
|
41
|
-
summary?: string;
|
|
42
|
-
message?: string;
|
|
43
|
-
found?: unknown;
|
|
44
|
-
property?: string;
|
|
45
|
-
expected?: string;
|
|
46
|
-
};
|
|
47
|
-
};
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
} & {
|
|
52
17
|
[x: string]: {
|
|
53
18
|
[x: string]: {
|
|
54
19
|
body: unknown;
|
|
@@ -56,15 +21,7 @@ export declare function createDocsMcpHandler(path?: string): import("elysia").de
|
|
|
56
21
|
query: unknown;
|
|
57
22
|
headers: unknown;
|
|
58
23
|
response: {
|
|
59
|
-
200:
|
|
60
|
-
jsonrpc: string;
|
|
61
|
-
error: {
|
|
62
|
-
code: import("@modelcontextprotocol/sdk/types.js").ErrorCode;
|
|
63
|
-
message: string;
|
|
64
|
-
data: string;
|
|
65
|
-
};
|
|
66
|
-
id: null;
|
|
67
|
-
};
|
|
24
|
+
200: Response;
|
|
68
25
|
};
|
|
69
26
|
};
|
|
70
27
|
};
|
|
@@ -79,15 +36,5 @@ export declare function createDocsMcpHandler(path?: string): import("elysia").de
|
|
|
79
36
|
resolve: {};
|
|
80
37
|
schema: {};
|
|
81
38
|
standaloneSchema: {};
|
|
82
|
-
response: {
|
|
83
|
-
200: {} | {
|
|
84
|
-
jsonrpc: string;
|
|
85
|
-
error: {
|
|
86
|
-
code: import("@modelcontextprotocol/sdk/types.js").ErrorCode;
|
|
87
|
-
message: string;
|
|
88
|
-
data: string;
|
|
89
|
-
};
|
|
90
|
-
id: null;
|
|
91
|
-
};
|
|
92
|
-
};
|
|
39
|
+
response: {};
|
|
93
40
|
}>;
|
|
@@ -2,72 +2,161 @@
|
|
|
2
2
|
// src/application/mcp/common.ts
|
|
3
3
|
import { PresentationRegistry } from "@contractspec/lib.contracts-spec/presentations";
|
|
4
4
|
import { createMcpServer } from "@contractspec/lib.contracts-runtime-server-mcp/provider-mcp";
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return {
|
|
10
|
-
log: (...args) => {
|
|
11
|
-
if (!isDebug)
|
|
12
|
-
return;
|
|
13
|
-
logger.info(toMessage(args));
|
|
14
|
-
},
|
|
15
|
-
info: (...args) => {
|
|
16
|
-
if (!isDebug)
|
|
17
|
-
return;
|
|
18
|
-
logger.info(toMessage(args));
|
|
19
|
-
},
|
|
20
|
-
warn: (...args) => {
|
|
21
|
-
logger.warn(toMessage(args));
|
|
22
|
-
},
|
|
23
|
-
error: (...args) => {
|
|
24
|
-
logger.error(toMessage(args));
|
|
25
|
-
},
|
|
26
|
-
debug: (...args) => {
|
|
27
|
-
if (!isDebug)
|
|
28
|
-
return;
|
|
29
|
-
logger.debug(toMessage(args));
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
}
|
|
5
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
6
|
+
import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js";
|
|
7
|
+
import { Elysia } from "elysia";
|
|
8
|
+
import { randomUUID } from "crypto";
|
|
33
9
|
var baseCtx = {
|
|
34
10
|
actor: "anonymous",
|
|
35
11
|
decide: async () => ({ effect: "allow" })
|
|
36
12
|
};
|
|
37
|
-
function
|
|
13
|
+
function createJsonRpcErrorResponse(status, code, message, data) {
|
|
14
|
+
return new Response(JSON.stringify({
|
|
15
|
+
jsonrpc: "2.0",
|
|
16
|
+
error: {
|
|
17
|
+
code,
|
|
18
|
+
message,
|
|
19
|
+
...data ? { data } : {}
|
|
20
|
+
},
|
|
21
|
+
id: null
|
|
22
|
+
}), {
|
|
23
|
+
status,
|
|
24
|
+
headers: {
|
|
25
|
+
"content-type": "application/json"
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function createSessionState({
|
|
38
30
|
logger,
|
|
39
|
-
path,
|
|
40
31
|
serverName,
|
|
41
32
|
ops,
|
|
42
33
|
resources,
|
|
43
34
|
prompts,
|
|
44
|
-
presentations
|
|
35
|
+
presentations,
|
|
36
|
+
stateful
|
|
45
37
|
}) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
serverInfo: {
|
|
51
|
-
name: serverName,
|
|
52
|
-
version: "1.0.0"
|
|
53
|
-
},
|
|
54
|
-
stateless: process.env.CONTRACTSPEC_MCP_STATEFUL !== "1",
|
|
55
|
-
enableJsonResponse: true,
|
|
38
|
+
const server = new McpServer({
|
|
39
|
+
name: serverName,
|
|
40
|
+
version: "1.0.0"
|
|
41
|
+
}, {
|
|
56
42
|
capabilities: {
|
|
57
43
|
tools: {},
|
|
58
44
|
resources: {},
|
|
59
45
|
prompts: {},
|
|
60
46
|
logging: {}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
logger.info("Setting up MCP server...");
|
|
50
|
+
createMcpServer(server, ops, resources, prompts, {
|
|
51
|
+
logger,
|
|
52
|
+
toolCtx: () => baseCtx,
|
|
53
|
+
promptCtx: () => ({ locale: "en" }),
|
|
54
|
+
resourceCtx: () => ({ locale: "en" }),
|
|
55
|
+
presentations: new PresentationRegistry(presentations)
|
|
56
|
+
});
|
|
57
|
+
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
58
|
+
sessionIdGenerator: stateful ? () => randomUUID() : undefined,
|
|
59
|
+
enableJsonResponse: true
|
|
60
|
+
});
|
|
61
|
+
return server.connect(transport).then(() => ({ server, transport }));
|
|
62
|
+
}
|
|
63
|
+
async function closeSessionState(state) {
|
|
64
|
+
await Promise.allSettled([state.transport.close(), state.server.close()]);
|
|
65
|
+
}
|
|
66
|
+
function toErrorMessage(error) {
|
|
67
|
+
return error instanceof Error ? error.stack ?? error.message : String(error);
|
|
68
|
+
}
|
|
69
|
+
function createMcpElysiaHandler({
|
|
70
|
+
logger,
|
|
71
|
+
path,
|
|
72
|
+
serverName,
|
|
73
|
+
ops,
|
|
74
|
+
resources,
|
|
75
|
+
prompts,
|
|
76
|
+
presentations
|
|
77
|
+
}) {
|
|
78
|
+
logger.info("Setting up MCP handler...");
|
|
79
|
+
const isStateful = process.env.CONTRACTSPEC_MCP_STATEFUL === "1";
|
|
80
|
+
const sessions = new Map;
|
|
81
|
+
async function handleStateless(request) {
|
|
82
|
+
const state = await createSessionState({
|
|
83
|
+
logger,
|
|
84
|
+
path,
|
|
85
|
+
serverName,
|
|
86
|
+
ops,
|
|
87
|
+
resources,
|
|
88
|
+
prompts,
|
|
89
|
+
presentations,
|
|
90
|
+
stateful: false
|
|
91
|
+
});
|
|
92
|
+
try {
|
|
93
|
+
return await state.transport.handleRequest(request);
|
|
94
|
+
} finally {
|
|
95
|
+
await closeSessionState(state);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async function closeSession(sessionId) {
|
|
99
|
+
const state = sessions.get(sessionId);
|
|
100
|
+
if (!state)
|
|
101
|
+
return;
|
|
102
|
+
sessions.delete(sessionId);
|
|
103
|
+
await closeSessionState(state);
|
|
104
|
+
}
|
|
105
|
+
async function handleStateful(request) {
|
|
106
|
+
const requestedSessionId = request.headers.get("mcp-session-id");
|
|
107
|
+
let state;
|
|
108
|
+
let createdState = false;
|
|
109
|
+
if (requestedSessionId) {
|
|
110
|
+
const existing = sessions.get(requestedSessionId);
|
|
111
|
+
if (!existing) {
|
|
112
|
+
return createJsonRpcErrorResponse(404, -32001, "Session not found");
|
|
113
|
+
}
|
|
114
|
+
state = existing;
|
|
115
|
+
} else {
|
|
116
|
+
state = await createSessionState({
|
|
65
117
|
logger,
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
118
|
+
path,
|
|
119
|
+
serverName,
|
|
120
|
+
ops,
|
|
121
|
+
resources,
|
|
122
|
+
prompts,
|
|
123
|
+
presentations,
|
|
124
|
+
stateful: true
|
|
125
|
+
});
|
|
126
|
+
createdState = true;
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
const response = await state.transport.handleRequest(request);
|
|
130
|
+
const activeSessionId = state.transport.sessionId;
|
|
131
|
+
if (activeSessionId && !sessions.has(activeSessionId)) {
|
|
132
|
+
sessions.set(activeSessionId, state);
|
|
133
|
+
}
|
|
134
|
+
if (request.method === "DELETE" && activeSessionId) {
|
|
135
|
+
await closeSession(activeSessionId);
|
|
136
|
+
} else if (!activeSessionId && createdState) {
|
|
137
|
+
await closeSessionState(state);
|
|
138
|
+
}
|
|
139
|
+
return response;
|
|
140
|
+
} catch (error) {
|
|
141
|
+
if (createdState) {
|
|
142
|
+
await closeSessionState(state);
|
|
143
|
+
}
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return new Elysia({ name: `mcp-${serverName}` }).all(path, async ({ request }) => {
|
|
148
|
+
try {
|
|
149
|
+
if (isStateful) {
|
|
150
|
+
return await handleStateful(request);
|
|
151
|
+
}
|
|
152
|
+
return await handleStateless(request);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
logger.error("Error handling MCP request", {
|
|
155
|
+
path,
|
|
156
|
+
method: request.method,
|
|
157
|
+
error: toErrorMessage(error)
|
|
70
158
|
});
|
|
159
|
+
return createJsonRpcErrorResponse(500, -32000, "Internal error");
|
|
71
160
|
}
|
|
72
161
|
});
|
|
73
162
|
}
|