xmcp 0.6.5 → 0.6.6
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/cli.js +45 -25
- package/dist/compiler/config/index.d.ts +59 -0
- package/dist/compiler/config/schemas/template.d.ts +40 -0
- package/dist/compiler/config/schemas/transport/stdio.d.ts +5 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4 -4
- package/dist/runtime/adapter-express.js +12 -12
- package/dist/runtime/adapter-nestjs.js +14 -14
- package/dist/runtime/adapter-nextjs.js +13 -13
- package/dist/runtime/http.js +20 -20
- package/dist/runtime/stdio.js +13 -13
- package/dist/types/tool.d.ts +1 -0
- package/package.json +1 -1
- package/src/compiler/config/__tests__/config.test.ts +36 -0
- package/src/compiler/config/injection.ts +63 -1
- package/src/compiler/config/schemas/template.ts +17 -0
- package/src/compiler/config/schemas/transport/stdio.ts +1 -0
- package/src/compiler/generate-tools-code.ts +21 -1
- package/src/compiler/get-bundler-config/get-injected-variables.ts +3 -0
- package/src/compiler/index.ts +99 -23
- package/src/compiler/start-http-server.ts +11 -4
- package/src/compiler/watcher-recovery.ts +72 -0
- package/src/index.ts +1 -0
- package/src/runtime/transports/stdio/index.ts +19 -0
- package/src/runtime/utils/server.ts +3 -5
- package/src/runtime/utils/tools.ts +22 -3
- package/src/runtime/utils/transformers/tool.ts +127 -9
- package/src/types/tool.ts +1 -0
|
@@ -7,17 +7,52 @@ import { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol";
|
|
|
7
7
|
import { ZodRawShape } from "zod/v3";
|
|
8
8
|
import { validateContent } from "../validators";
|
|
9
9
|
|
|
10
|
+
function validateAgainstOutputSchema(
|
|
11
|
+
data: Record<string, unknown>,
|
|
12
|
+
outputSchema: ZodRawShape,
|
|
13
|
+
toolName: string,
|
|
14
|
+
errorPrefix: string
|
|
15
|
+
): void {
|
|
16
|
+
if (data === null || typeof data !== "object" || Array.isArray(data)) {
|
|
17
|
+
throw new Error(
|
|
18
|
+
`Tool "${toolName}" ${errorPrefix}: expected a plain object.`
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const outputKeys = new Set(Object.keys(outputSchema));
|
|
23
|
+
const extraKeys = Object.keys(data).filter((key) => !outputKeys.has(key));
|
|
24
|
+
if (extraKeys.length > 0) {
|
|
25
|
+
throw new Error(
|
|
26
|
+
`Tool "${toolName}" ${errorPrefix}: unrecognized key(s): ${extraKeys.join(", ")}`
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
for (const [fieldName, fieldSchema] of Object.entries(outputSchema)) {
|
|
31
|
+
try {
|
|
32
|
+
fieldSchema.parse((data as any)[fieldName]);
|
|
33
|
+
} catch (error: any) {
|
|
34
|
+
throw new Error(
|
|
35
|
+
`Tool "${toolName}" ${errorPrefix}: ${error?.message ?? `invalid field "${fieldName}"`}`
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
10
41
|
/**
|
|
11
42
|
* Type for the original tool handler that users write
|
|
12
43
|
*/
|
|
44
|
+
export type UserToolResponse =
|
|
45
|
+
| CallToolResult
|
|
46
|
+
| string
|
|
47
|
+
| number
|
|
48
|
+
| Record<string, unknown>;
|
|
49
|
+
|
|
13
50
|
export type UserToolHandler = (
|
|
14
51
|
args: ZodRawShape,
|
|
15
52
|
extra: RequestHandlerExtra<ServerRequest, ServerNotification>
|
|
16
53
|
) =>
|
|
17
|
-
|
|
|
18
|
-
|
|
|
19
|
-
| number
|
|
20
|
-
| Promise<CallToolResult | string | number>;
|
|
54
|
+
| UserToolResponse
|
|
55
|
+
| Promise<UserToolResponse>;
|
|
21
56
|
|
|
22
57
|
/**
|
|
23
58
|
* Type for the transformed handler that the MCP server expects
|
|
@@ -53,13 +88,15 @@ function hasUIMeta(meta?: Record<string, any>): boolean {
|
|
|
53
88
|
*/
|
|
54
89
|
export function transformToolHandler(
|
|
55
90
|
handler: UserToolHandler,
|
|
56
|
-
meta?: Record<string, any
|
|
91
|
+
meta?: Record<string, any>,
|
|
92
|
+
outputSchema?: ZodRawShape,
|
|
93
|
+
toolName = "unknown-tool"
|
|
57
94
|
): McpToolHandler {
|
|
58
95
|
return async (
|
|
59
96
|
args: ZodRawShape,
|
|
60
97
|
extra: RequestHandlerExtra<ServerRequest, ServerNotification>
|
|
61
98
|
): Promise<CallToolResult> => {
|
|
62
|
-
let response = handler(args, extra);
|
|
99
|
+
let response: any = handler(args, extra);
|
|
63
100
|
|
|
64
101
|
// only await if it's actually a promise
|
|
65
102
|
if (response instanceof Promise) {
|
|
@@ -67,6 +104,30 @@ export function transformToolHandler(
|
|
|
67
104
|
}
|
|
68
105
|
|
|
69
106
|
if (typeof response === "string" || typeof response === "number") {
|
|
107
|
+
if (outputSchema) {
|
|
108
|
+
const outputSchemaEntries = Object.entries(outputSchema);
|
|
109
|
+
if (outputSchemaEntries.length === 1) {
|
|
110
|
+
const [fieldName, fieldSchema] = outputSchemaEntries[0];
|
|
111
|
+
try {
|
|
112
|
+
fieldSchema.parse(response);
|
|
113
|
+
return {
|
|
114
|
+
content: [
|
|
115
|
+
{
|
|
116
|
+
type: "text",
|
|
117
|
+
text: typeof response === "number" ? `${response}` : response,
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
structuredContent: {
|
|
121
|
+
[fieldName]: response,
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
} catch {
|
|
125
|
+
// Backward compatible fallback: keep content-only when primitive
|
|
126
|
+
// output does not match outputSchema.
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
70
131
|
// Check if we have widget metadata to attach
|
|
71
132
|
const hasWidgetMeta = hasUIMeta(meta);
|
|
72
133
|
|
|
@@ -98,6 +159,30 @@ export function transformToolHandler(
|
|
|
98
159
|
};
|
|
99
160
|
}
|
|
100
161
|
|
|
162
|
+
// With outputSchema declared, allow returning a plain object as shorthand
|
|
163
|
+
// for `{ structuredContent: ... }`.
|
|
164
|
+
if (
|
|
165
|
+
outputSchema &&
|
|
166
|
+
response &&
|
|
167
|
+
typeof response === "object" &&
|
|
168
|
+
!Array.isArray(response) &&
|
|
169
|
+
!("structuredContent" in response) &&
|
|
170
|
+
!("content" in response && Array.isArray((response as any).content)) &&
|
|
171
|
+
!("_meta" in response) &&
|
|
172
|
+
!("isError" in response)
|
|
173
|
+
) {
|
|
174
|
+
// Validate the response against outputSchema before wrapping.
|
|
175
|
+
validateAgainstOutputSchema(
|
|
176
|
+
response as Record<string, unknown>,
|
|
177
|
+
outputSchema,
|
|
178
|
+
toolName,
|
|
179
|
+
"returned data that does not match outputSchema"
|
|
180
|
+
);
|
|
181
|
+
response = {
|
|
182
|
+
structuredContent: response,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
101
186
|
// Check if response has _meta but no content (special case for widget metadata)
|
|
102
187
|
if (
|
|
103
188
|
response &&
|
|
@@ -166,10 +251,11 @@ export function transformToolHandler(
|
|
|
166
251
|
}
|
|
167
252
|
|
|
168
253
|
// Check if response has at least one of: content or structuredContent
|
|
169
|
-
|
|
254
|
+
let hasContent = "content" in response && Array.isArray(response.content);
|
|
170
255
|
const hasStructuredContent = "structuredContent" in response;
|
|
256
|
+
const isError = "isError" in response && (response as any).isError === true;
|
|
171
257
|
|
|
172
|
-
if (!hasContent && !hasStructuredContent) {
|
|
258
|
+
if (!hasContent && !hasStructuredContent && !isError) {
|
|
173
259
|
const responseValue = JSON.stringify(response, null, 2);
|
|
174
260
|
|
|
175
261
|
throw new Error(
|
|
@@ -196,6 +282,38 @@ export function transformToolHandler(
|
|
|
196
282
|
);
|
|
197
283
|
}
|
|
198
284
|
|
|
285
|
+
// Auto-generate fallback content for isError responses without content
|
|
286
|
+
if (isError && !hasContent && !hasStructuredContent) {
|
|
287
|
+
const { isError: _, ...rest } = response as Record<string, unknown>;
|
|
288
|
+
const errorText =
|
|
289
|
+
Object.keys(rest).length > 0
|
|
290
|
+
? `Tool execution failed: ${JSON.stringify(rest)}`
|
|
291
|
+
: "Tool execution failed";
|
|
292
|
+
response.content = [{ type: "text", text: errorText }];
|
|
293
|
+
hasContent = true;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Validate structuredContent against outputSchema if present.
|
|
297
|
+
if (outputSchema && hasStructuredContent && !isError) {
|
|
298
|
+
validateAgainstOutputSchema(
|
|
299
|
+
(response as any).structuredContent as Record<string, unknown>,
|
|
300
|
+
outputSchema,
|
|
301
|
+
toolName,
|
|
302
|
+
"returned structuredContent that does not match outputSchema"
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Auto-generate text fallback for clients that only render content.
|
|
307
|
+
if (!hasContent && hasStructuredContent) {
|
|
308
|
+
response.content = [
|
|
309
|
+
{
|
|
310
|
+
type: "text",
|
|
311
|
+
text: JSON.stringify((response as any).structuredContent),
|
|
312
|
+
},
|
|
313
|
+
];
|
|
314
|
+
hasContent = true;
|
|
315
|
+
}
|
|
316
|
+
|
|
199
317
|
// validate each content item if content is present
|
|
200
318
|
if (hasContent) {
|
|
201
319
|
for (let i = 0; i < response.content.length; i++) {
|
|
@@ -243,6 +361,6 @@ export function transformToolHandler(
|
|
|
243
361
|
}
|
|
244
362
|
}
|
|
245
363
|
|
|
246
|
-
return response;
|
|
364
|
+
return response as CallToolResult;
|
|
247
365
|
};
|
|
248
366
|
}
|
package/src/types/tool.ts
CHANGED
|
@@ -33,6 +33,7 @@ export interface ToolMetadata {
|
|
|
33
33
|
type CompatibleZodType = z.ZodTypeAny | ZodTypeV4<unknown>;
|
|
34
34
|
|
|
35
35
|
export type ToolSchema = Record<string, CompatibleZodType>;
|
|
36
|
+
export type ToolOutputSchema = Record<string, CompatibleZodType>;
|
|
36
37
|
|
|
37
38
|
// The ToolExtraArguments type is equivalent to Parameters<ToolCallback<undefined>>[0] from @modelcontextprotocol/sdk but fully resolved to avoid external type dependencies.
|
|
38
39
|
/**
|