@contractspec/bundle.library 2.5.0 → 2.6.1
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 +33 -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 +24 -25
- package/src/application/mcp/common.ts +180 -87
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contractspec/bundle.library",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"clean": "rm -rf dist",
|
|
@@ -2052,43 +2052,42 @@
|
|
|
2052
2052
|
"react": "19.2.4"
|
|
2053
2053
|
},
|
|
2054
2054
|
"dependencies": {
|
|
2055
|
-
"@apollo/client": "^4.1.
|
|
2056
|
-
"@contractspec/lib.contracts-spec": "2.
|
|
2057
|
-
"@contractspec/lib.contracts-library": "2.
|
|
2058
|
-
"@contractspec/lib.contracts-runtime-server-mcp": "2.
|
|
2059
|
-
"@contractspec/lib.design-system": "2.
|
|
2060
|
-
"@contractspec/lib.example-shared-ui": "2.
|
|
2061
|
-
"@contractspec/lib.logger": "2.
|
|
2062
|
-
"@contractspec/lib.runtime-sandbox": "1.
|
|
2063
|
-
"@contractspec/lib.schema": "2.
|
|
2064
|
-
"@contractspec/lib.ui-kit-web": "2.
|
|
2065
|
-
"@contractspec/lib.ui-link": "2.
|
|
2066
|
-
"@contractspec/module.examples": "2.
|
|
2055
|
+
"@apollo/client": "^4.1.6",
|
|
2056
|
+
"@contractspec/lib.contracts-spec": "2.6.0",
|
|
2057
|
+
"@contractspec/lib.contracts-library": "2.6.0",
|
|
2058
|
+
"@contractspec/lib.contracts-runtime-server-mcp": "2.6.0",
|
|
2059
|
+
"@contractspec/lib.design-system": "2.6.1",
|
|
2060
|
+
"@contractspec/lib.example-shared-ui": "2.6.1",
|
|
2061
|
+
"@contractspec/lib.logger": "2.6.0",
|
|
2062
|
+
"@contractspec/lib.runtime-sandbox": "1.6.0",
|
|
2063
|
+
"@contractspec/lib.schema": "2.6.0",
|
|
2064
|
+
"@contractspec/lib.ui-kit-web": "2.6.0",
|
|
2065
|
+
"@contractspec/lib.ui-link": "2.6.0",
|
|
2066
|
+
"@contractspec/module.examples": "2.6.1",
|
|
2067
2067
|
"@dnd-kit/core": "^6.1.0",
|
|
2068
2068
|
"@dnd-kit/sortable": "^10.0.0",
|
|
2069
2069
|
"@dnd-kit/utilities": "^3.2.2",
|
|
2070
2070
|
"@electric-sql/pglite": "^0.3.14",
|
|
2071
|
-
"@scaleway/sdk": "^3.2.
|
|
2071
|
+
"@scaleway/sdk": "^3.2.1",
|
|
2072
2072
|
"@scaleway/sdk-client": "^2.1.0",
|
|
2073
2073
|
"drizzle-orm": "^0.45.1",
|
|
2074
|
-
"
|
|
2075
|
-
"
|
|
2076
|
-
"
|
|
2077
|
-
"posthog-
|
|
2078
|
-
"posthog-node": "^5.24.15",
|
|
2074
|
+
"framer-motion": "^12.34.3",
|
|
2075
|
+
"lucide-react": "^0.575.0",
|
|
2076
|
+
"posthog-js": "^1.353.0",
|
|
2077
|
+
"posthog-node": "^5.25.0",
|
|
2079
2078
|
"posthog-react-native": "^4.34.0",
|
|
2080
|
-
"react-hook-form": "^7.
|
|
2079
|
+
"react-hook-form": "^7.71.2",
|
|
2081
2080
|
"zod": "^4.3.5",
|
|
2082
|
-
"@contractspec/lib.contracts-integrations": "2.
|
|
2083
|
-
"@contractspec/lib.contracts-runtime-server-rest": "2.
|
|
2084
|
-
"@contractspec/lib.contracts-runtime-server-graphql": "2.
|
|
2081
|
+
"@contractspec/lib.contracts-integrations": "2.6.0",
|
|
2082
|
+
"@contractspec/lib.contracts-runtime-server-rest": "2.6.0",
|
|
2083
|
+
"@contractspec/lib.contracts-runtime-server-graphql": "2.6.0"
|
|
2085
2084
|
},
|
|
2086
2085
|
"devDependencies": {
|
|
2087
2086
|
"@types/react": "~19.2.14",
|
|
2088
|
-
"@contractspec/tool.typescript": "2.
|
|
2087
|
+
"@contractspec/tool.typescript": "2.6.0",
|
|
2089
2088
|
"typescript": "^5.9.3",
|
|
2090
2089
|
"@types/bun": "~1.3.9",
|
|
2091
|
-
"@contractspec/tool.bun": "2.
|
|
2090
|
+
"@contractspec/tool.bun": "2.6.0"
|
|
2092
2091
|
},
|
|
2093
2092
|
"publishConfig": {
|
|
2094
2093
|
"access": "public",
|
|
@@ -6,36 +6,11 @@ import type {
|
|
|
6
6
|
import { PresentationRegistry } from '@contractspec/lib.contracts-spec/presentations';
|
|
7
7
|
import { createMcpServer } from '@contractspec/lib.contracts-runtime-server-mcp/provider-mcp';
|
|
8
8
|
import type { PresentationSpec } from '@contractspec/lib.contracts-spec/presentations';
|
|
9
|
-
import {
|
|
9
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
10
|
+
import { WebStandardStreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js';
|
|
11
|
+
import { Elysia } from 'elysia';
|
|
10
12
|
import { Logger } from '@contractspec/lib.logger';
|
|
11
|
-
|
|
12
|
-
function createConsoleLikeLogger(logger: Logger) {
|
|
13
|
-
const isDebug = process.env.CONTRACTSPEC_MCP_DEBUG === '1';
|
|
14
|
-
|
|
15
|
-
const toMessage = (args: unknown[]) =>
|
|
16
|
-
args.map((a) => (typeof a === 'string' ? a : JSON.stringify(a))).join(' ');
|
|
17
|
-
|
|
18
|
-
return {
|
|
19
|
-
log: (...args: unknown[]) => {
|
|
20
|
-
if (!isDebug) return;
|
|
21
|
-
logger.info(toMessage(args));
|
|
22
|
-
},
|
|
23
|
-
info: (...args: unknown[]) => {
|
|
24
|
-
if (!isDebug) return;
|
|
25
|
-
logger.info(toMessage(args));
|
|
26
|
-
},
|
|
27
|
-
warn: (...args: unknown[]) => {
|
|
28
|
-
logger.warn(toMessage(args));
|
|
29
|
-
},
|
|
30
|
-
error: (...args: unknown[]) => {
|
|
31
|
-
logger.error(toMessage(args));
|
|
32
|
-
},
|
|
33
|
-
debug: (...args: unknown[]) => {
|
|
34
|
-
if (!isDebug) return;
|
|
35
|
-
logger.debug(toMessage(args));
|
|
36
|
-
},
|
|
37
|
-
};
|
|
38
|
-
}
|
|
13
|
+
import { randomUUID } from 'node:crypto';
|
|
39
14
|
|
|
40
15
|
interface McpHttpHandlerConfig {
|
|
41
16
|
path: string;
|
|
@@ -52,75 +27,193 @@ const baseCtx = {
|
|
|
52
27
|
decide: async () => ({ effect: 'allow' as const }),
|
|
53
28
|
};
|
|
54
29
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
// resources,
|
|
60
|
-
// prompts,
|
|
61
|
-
// presentationsV2,
|
|
62
|
-
// }: McpHttpHandlerConfig) {
|
|
63
|
-
// return createMcpHandler(
|
|
64
|
-
// (server) => {
|
|
65
|
-
// createMcpServer(server, ops, resources, prompts, {
|
|
66
|
-
// toolCtx: () => baseCtx,
|
|
67
|
-
// promptCtx: () => ({ locale: 'en' }),
|
|
68
|
-
// resourceCtx: () => ({ locale: 'en' }),
|
|
69
|
-
// presentationsV2,
|
|
70
|
-
// });
|
|
71
|
-
// },
|
|
72
|
-
// {
|
|
73
|
-
// serverInfo: {
|
|
74
|
-
// name: serverName,
|
|
75
|
-
// version: '1.0.0',
|
|
76
|
-
// },
|
|
77
|
-
// },
|
|
78
|
-
// {
|
|
79
|
-
// // basePath: path,
|
|
80
|
-
// disableSse: true,
|
|
81
|
-
// verboseLogs: true,
|
|
82
|
-
// }
|
|
83
|
-
// );
|
|
84
|
-
// }
|
|
30
|
+
interface McpSessionState {
|
|
31
|
+
server: McpServer;
|
|
32
|
+
transport: WebStandardStreamableHTTPServerTransport;
|
|
33
|
+
}
|
|
85
34
|
|
|
86
|
-
|
|
35
|
+
function createJsonRpcErrorResponse(
|
|
36
|
+
status: number,
|
|
37
|
+
code: number,
|
|
38
|
+
message: string,
|
|
39
|
+
data?: string
|
|
40
|
+
) {
|
|
41
|
+
return new Response(
|
|
42
|
+
JSON.stringify({
|
|
43
|
+
jsonrpc: '2.0',
|
|
44
|
+
error: {
|
|
45
|
+
code,
|
|
46
|
+
message,
|
|
47
|
+
...(data ? { data } : {}),
|
|
48
|
+
},
|
|
49
|
+
id: null,
|
|
50
|
+
}),
|
|
51
|
+
{
|
|
52
|
+
status,
|
|
53
|
+
headers: {
|
|
54
|
+
'content-type': 'application/json',
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function createSessionState({
|
|
87
61
|
logger,
|
|
88
|
-
path,
|
|
89
62
|
serverName,
|
|
90
63
|
ops,
|
|
91
64
|
resources,
|
|
92
65
|
prompts,
|
|
93
66
|
presentations,
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
// Do NOT use console.* in production paths; adapt to console-like interface for the plugin.
|
|
99
|
-
logger: createConsoleLikeLogger(logger),
|
|
100
|
-
serverInfo: {
|
|
67
|
+
stateful,
|
|
68
|
+
}: McpHttpHandlerConfig & { stateful: boolean }): Promise<McpSessionState> {
|
|
69
|
+
const server = new McpServer(
|
|
70
|
+
{
|
|
101
71
|
name: serverName,
|
|
102
72
|
version: '1.0.0',
|
|
103
73
|
},
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
74
|
+
{
|
|
75
|
+
capabilities: {
|
|
76
|
+
tools: {},
|
|
77
|
+
resources: {},
|
|
78
|
+
prompts: {},
|
|
79
|
+
logging: {},
|
|
80
|
+
},
|
|
81
|
+
}
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
logger.info('Setting up MCP server...');
|
|
85
|
+
createMcpServer(server, ops, resources, prompts, {
|
|
86
|
+
logger,
|
|
87
|
+
toolCtx: () => baseCtx,
|
|
88
|
+
promptCtx: () => ({ locale: 'en' }),
|
|
89
|
+
resourceCtx: () => ({ locale: 'en' }),
|
|
90
|
+
presentations: new PresentationRegistry(presentations),
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
94
|
+
sessionIdGenerator: stateful ? () => randomUUID() : undefined,
|
|
108
95
|
enableJsonResponse: true,
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
return server.connect(transport).then(() => ({ server, transport }));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function closeSessionState(state: McpSessionState) {
|
|
102
|
+
await Promise.allSettled([state.transport.close(), state.server.close()]);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function toErrorMessage(error: unknown) {
|
|
106
|
+
return error instanceof Error
|
|
107
|
+
? (error.stack ?? error.message)
|
|
108
|
+
: String(error);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function createMcpElysiaHandler({
|
|
112
|
+
logger,
|
|
113
|
+
path,
|
|
114
|
+
serverName,
|
|
115
|
+
ops,
|
|
116
|
+
resources,
|
|
117
|
+
prompts,
|
|
118
|
+
presentations,
|
|
119
|
+
}: McpHttpHandlerConfig) {
|
|
120
|
+
logger.info('Setting up MCP handler...');
|
|
121
|
+
|
|
122
|
+
const isStateful = process.env.CONTRACTSPEC_MCP_STATEFUL === '1';
|
|
123
|
+
const sessions = new Map<string, McpSessionState>();
|
|
124
|
+
|
|
125
|
+
async function handleStateless(request: Request) {
|
|
126
|
+
const state = await createSessionState({
|
|
127
|
+
logger,
|
|
128
|
+
path,
|
|
129
|
+
serverName,
|
|
130
|
+
ops,
|
|
131
|
+
resources,
|
|
132
|
+
prompts,
|
|
133
|
+
presentations,
|
|
134
|
+
stateful: false,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
return await state.transport.handleRequest(request);
|
|
139
|
+
} finally {
|
|
140
|
+
await closeSessionState(state);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function closeSession(sessionId: string) {
|
|
145
|
+
const state = sessions.get(sessionId);
|
|
146
|
+
if (!state) return;
|
|
147
|
+
sessions.delete(sessionId);
|
|
148
|
+
await closeSessionState(state);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function handleStateful(request: Request) {
|
|
152
|
+
const requestedSessionId = request.headers.get('mcp-session-id');
|
|
153
|
+
let state: McpSessionState;
|
|
154
|
+
let createdState = false;
|
|
155
|
+
|
|
156
|
+
if (requestedSessionId) {
|
|
157
|
+
const existing = sessions.get(requestedSessionId);
|
|
158
|
+
if (!existing) {
|
|
159
|
+
return createJsonRpcErrorResponse(404, -32001, 'Session not found');
|
|
160
|
+
}
|
|
161
|
+
state = existing;
|
|
162
|
+
} else {
|
|
163
|
+
state = await createSessionState({
|
|
118
164
|
logger,
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
165
|
+
path,
|
|
166
|
+
serverName,
|
|
167
|
+
ops,
|
|
168
|
+
resources,
|
|
169
|
+
prompts,
|
|
170
|
+
presentations,
|
|
171
|
+
stateful: true,
|
|
123
172
|
});
|
|
124
|
-
|
|
125
|
-
|
|
173
|
+
createdState = true;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
const response = await state.transport.handleRequest(request);
|
|
178
|
+
const activeSessionId = state.transport.sessionId;
|
|
179
|
+
|
|
180
|
+
if (activeSessionId && !sessions.has(activeSessionId)) {
|
|
181
|
+
sessions.set(activeSessionId, state);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (request.method === 'DELETE' && activeSessionId) {
|
|
185
|
+
await closeSession(activeSessionId);
|
|
186
|
+
} else if (!activeSessionId && createdState) {
|
|
187
|
+
await closeSessionState(state);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return response;
|
|
191
|
+
} catch (error) {
|
|
192
|
+
if (createdState) {
|
|
193
|
+
await closeSessionState(state);
|
|
194
|
+
}
|
|
195
|
+
throw error;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return new Elysia({ name: `mcp-${serverName}` }).all(
|
|
200
|
+
path,
|
|
201
|
+
async ({ request }) => {
|
|
202
|
+
try {
|
|
203
|
+
if (isStateful) {
|
|
204
|
+
return await handleStateful(request);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return await handleStateless(request);
|
|
208
|
+
} catch (error) {
|
|
209
|
+
logger.error('Error handling MCP request', {
|
|
210
|
+
path,
|
|
211
|
+
method: request.method,
|
|
212
|
+
error: toErrorMessage(error),
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
return createJsonRpcErrorResponse(500, -32000, 'Internal error');
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
);
|
|
126
219
|
}
|