xmcp 0.6.5 → 0.6.7
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 +32 -0
- package/dist/cli.js +58 -40
- package/dist/compiler/config/index.d.ts +106 -47
- package/dist/compiler/config/schemas/template.d.ts +40 -0
- package/dist/compiler/config/schemas/transport/http.d.ts +82 -82
- package/dist/compiler/config/schemas/transport/stdio.d.ts +5 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +5 -5
- package/dist/runtime/adapter-express.js +40 -23
- package/dist/runtime/adapter-nestjs.js +40 -23
- package/dist/runtime/adapter-nextjs.js +39 -22
- package/dist/runtime/contexts/http-request-context.d.ts +3 -1
- package/dist/runtime/headers.js +1 -1
- package/dist/runtime/http.js +48 -31
- package/dist/runtime/stdio.js +19 -13
- package/dist/runtime/utils/elicitation.d.ts +4 -0
- package/dist/runtime/utils/ext-apps-registry.d.ts +132 -0
- package/dist/runtime/utils/path-to-tool-name.d.ts +20 -0
- package/dist/runtime/utils/prompt-loader.d.ts +11 -0
- package/dist/runtime/utils/prompts.d.ts +8 -0
- package/dist/runtime/utils/react/generate-html.d.ts +1 -0
- package/dist/runtime/utils/react/index.d.ts +4 -0
- package/dist/runtime/utils/resource-loader.d.ts +11 -0
- package/dist/runtime/utils/resources.d.ts +4 -0
- package/dist/runtime/utils/server.d.ts +35 -0
- package/dist/runtime/utils/tool-loader.d.ts +11 -0
- package/dist/runtime/utils/tools.d.ts +11 -0
- package/dist/runtime/utils/transformers/prompt.d.ts +32 -0
- package/dist/runtime/utils/transformers/resource.d.ts +26 -0
- package/dist/runtime/utils/transformers/tool.d.ts +30 -0
- package/dist/runtime/utils/ui/flatten-meta.d.ts +6 -0
- package/dist/runtime/utils/ui/split-meta.d.ts +17 -0
- package/dist/runtime/utils/utils/resource-uri-composer.d.ts +60 -0
- package/dist/runtime/utils/validators/index.d.ts +13 -0
- package/dist/types/client-info.d.ts +12 -0
- package/dist/types/tool.d.ts +70 -14
- package/dist/utils/context.d.ts +1 -1
- package/package.json +9 -1
- package/src/client/index.ts +4 -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 +24 -6
- package/src/compiler/get-bundler-config/get-injected-variables.ts +3 -0
- package/src/compiler/get-bundler-config/plugins/index.ts +0 -1
- package/src/compiler/index.ts +123 -30
- package/src/compiler/start-http-server.ts +11 -4
- package/src/compiler/watcher-recovery.ts +72 -0
- package/src/index.ts +9 -1
- package/src/runtime/adapters/express/index.ts +45 -47
- package/src/runtime/adapters/nestjs/xmcp.service.ts +4 -3
- package/src/runtime/adapters/nextjs/handler/node-to-web-adapter.ts +40 -6
- package/src/runtime/adapters/nextjs/handler/request-converter.ts +20 -0
- package/src/runtime/adapters/nextjs/handler/server-lifecycle.ts +8 -5
- package/src/runtime/adapters/nextjs/index.ts +41 -25
- package/src/runtime/contexts/http-request-context.ts +2 -0
- package/src/runtime/headers.ts +37 -2
- package/src/runtime/platforms/cloudflare/cors.ts +0 -1
- package/src/runtime/platforms/cloudflare/worker.ts +46 -44
- package/src/runtime/transports/http/index.ts +0 -4
- package/src/runtime/transports/http/stateless-streamable-http.ts +131 -233
- package/src/runtime/transports/stdio/index.ts +18 -1
- package/src/runtime/utils/__tests__/client-info.test.ts +80 -0
- package/src/runtime/utils/__tests__/tool-extra.test.ts +110 -0
- package/src/runtime/utils/client-info.ts +71 -0
- package/src/runtime/utils/elicitation.ts +396 -0
- package/src/runtime/utils/prompt-loader.ts +151 -0
- package/src/runtime/utils/resource-loader.ts +151 -0
- package/src/runtime/utils/resources.ts +0 -8
- package/src/runtime/utils/server.ts +30 -42
- package/src/runtime/utils/tool-loader.ts +149 -0
- package/src/runtime/utils/tools.ts +31 -9
- package/src/runtime/utils/transformers/tool.ts +149 -10
- package/src/types/client-info.ts +12 -0
- package/src/types/injected.d.ts +62 -0
- package/src/types/tool.ts +100 -17
- package/src/utils/context.ts +2 -2
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type { ResourceFile } from "./server";
|
|
2
|
+
|
|
3
|
+
const EMPTY_RESOURCE_FILE_MESSAGE = "File is empty.";
|
|
4
|
+
const MISSING_DEFAULT_EXPORT_MESSAGE =
|
|
5
|
+
"File does not export a default resource handler.";
|
|
6
|
+
const INVALID_DEFAULT_EXPORT_MESSAGE =
|
|
7
|
+
"Default export must be a resource handler function.";
|
|
8
|
+
|
|
9
|
+
type ResourceLoadIssue = {
|
|
10
|
+
path: string;
|
|
11
|
+
message: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
function createResourceLoadIssue(path: string): ResourceLoadIssue {
|
|
15
|
+
return {
|
|
16
|
+
path,
|
|
17
|
+
message: EMPTY_RESOURCE_FILE_MESSAGE,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function createInvalidResourceImplementationError(path: string): Error {
|
|
22
|
+
return new Error(
|
|
23
|
+
`[xmcp] Invalid resource file: ${path}\n -> ${INVALID_DEFAULT_EXPORT_MESSAGE}`
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function classifyResourceModule(
|
|
28
|
+
resourceModule: unknown,
|
|
29
|
+
path: string
|
|
30
|
+
): "empty" | "missing-default" | "valid" {
|
|
31
|
+
if (typeof resourceModule !== "object" || resourceModule === null) {
|
|
32
|
+
throw createInvalidResourceImplementationError(path);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const moduleRecord = resourceModule as Record<string, unknown>;
|
|
36
|
+
const keys = Object.keys(moduleRecord);
|
|
37
|
+
const defaultExport = moduleRecord.default;
|
|
38
|
+
const nonInteropKeys = keys.filter((key) => key !== "__esModule");
|
|
39
|
+
|
|
40
|
+
if (
|
|
41
|
+
keys.length === 0 ||
|
|
42
|
+
(nonInteropKeys.length === 1 &&
|
|
43
|
+
nonInteropKeys[0] === "default" &&
|
|
44
|
+
typeof defaultExport === "object" &&
|
|
45
|
+
defaultExport !== null &&
|
|
46
|
+
Object.keys(defaultExport as Record<string, unknown>).length === 0)
|
|
47
|
+
) {
|
|
48
|
+
return "empty";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!("default" in moduleRecord) || moduleRecord.default === undefined) {
|
|
52
|
+
return "missing-default";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (typeof moduleRecord.default !== "function") {
|
|
56
|
+
throw createInvalidResourceImplementationError(path);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return "valid";
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function toResourceFile(resourceModule: unknown): ResourceFile {
|
|
63
|
+
return resourceModule as ResourceFile;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function loadResourceModules(
|
|
67
|
+
loaders: Record<string, () => Promise<unknown>>
|
|
68
|
+
) {
|
|
69
|
+
const resourceModules = new Map<string, ResourceFile>();
|
|
70
|
+
const skippedResources: ResourceLoadIssue[] = [];
|
|
71
|
+
|
|
72
|
+
const results = await Promise.all(
|
|
73
|
+
Object.entries(loaders).map(async ([path, loadResource]) => {
|
|
74
|
+
const resourceModule = await loadResource();
|
|
75
|
+
const classification = classifyResourceModule(resourceModule, path);
|
|
76
|
+
|
|
77
|
+
if (classification === "empty") {
|
|
78
|
+
return {
|
|
79
|
+
skipped: true as const,
|
|
80
|
+
issue: createResourceLoadIssue(path),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (classification === "missing-default") {
|
|
85
|
+
return {
|
|
86
|
+
skipped: true as const,
|
|
87
|
+
issue: {
|
|
88
|
+
path,
|
|
89
|
+
message: MISSING_DEFAULT_EXPORT_MESSAGE,
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
skipped: false as const,
|
|
96
|
+
path,
|
|
97
|
+
resourceModule: toResourceFile(resourceModule),
|
|
98
|
+
};
|
|
99
|
+
})
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
for (const result of results) {
|
|
103
|
+
if (result.skipped) {
|
|
104
|
+
skippedResources.push(result.issue);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
resourceModules.set(result.path, result.resourceModule);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
resourceModules,
|
|
113
|
+
skippedResources,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function createResourceLoadIssueReporter(
|
|
118
|
+
logger: Pick<Console, "warn"> = console
|
|
119
|
+
) {
|
|
120
|
+
let lastReportedSummaryKey = "";
|
|
121
|
+
|
|
122
|
+
return (skippedResources: ResourceLoadIssue[]) => {
|
|
123
|
+
const summaryKey = skippedResources
|
|
124
|
+
.map(({ path, message }) => `${path}:${message}`)
|
|
125
|
+
.sort()
|
|
126
|
+
.join("|");
|
|
127
|
+
|
|
128
|
+
if (summaryKey === lastReportedSummaryKey) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
lastReportedSummaryKey = summaryKey;
|
|
133
|
+
|
|
134
|
+
if (skippedResources.length === 0) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
skippedResources.forEach(({ path, message }) => {
|
|
139
|
+
logger.warn(
|
|
140
|
+
`[xmcp] Failed to load resource file: ${path}\n -> ${message}`
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const count = skippedResources.length;
|
|
145
|
+
logger.warn(
|
|
146
|
+
`[xmcp] ${count} resource${count === 1 ? "" : "s"} skipped due to empty files or missing default exports`
|
|
147
|
+
);
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export const reportResourceLoadIssues = createResourceLoadIssueReporter();
|
|
@@ -14,14 +14,6 @@ import { generateUIHTML } from "./react/generate-html";
|
|
|
14
14
|
import { pathToToolNameMd5, pathToToolNameDjb2 } from "./path-to-tool-name";
|
|
15
15
|
import { uIResourceRegistry } from "./ext-apps-registry";
|
|
16
16
|
|
|
17
|
-
// Client bundles can be injected at compile time for Cloudflare Workers
|
|
18
|
-
// This variable is defined by DefinePlugin at compile time
|
|
19
|
-
declare const INJECTED_CLIENT_BUNDLES:
|
|
20
|
-
| Record<string, { js: string; css?: string }>
|
|
21
|
-
| undefined;
|
|
22
|
-
|
|
23
|
-
declare const IS_CLOUDFLARE: boolean;
|
|
24
|
-
|
|
25
17
|
/**
|
|
26
18
|
* Get the appropriate pathToToolName function based on runtime environment.
|
|
27
19
|
* - For Cloudflare (INJECTED_CLIENT_BUNDLES defined): Use djb2 hash
|
|
@@ -11,10 +11,17 @@ import { ZodRawShape } from "zod/v3";
|
|
|
11
11
|
import { addResourcesToServer } from "./resources";
|
|
12
12
|
import { ResourceMetadata } from "@/types/resource";
|
|
13
13
|
import { uIResourceRegistry } from "./ext-apps-registry";
|
|
14
|
+
import { loadPromptModules, reportPromptLoadIssues } from "./prompt-loader";
|
|
15
|
+
import {
|
|
16
|
+
loadResourceModules,
|
|
17
|
+
reportResourceLoadIssues,
|
|
18
|
+
} from "./resource-loader";
|
|
19
|
+
import { loadToolModules, reportToolLoadIssues } from "./tool-loader";
|
|
14
20
|
|
|
15
21
|
export type ToolFile = {
|
|
16
22
|
metadata: ToolMetadata;
|
|
17
23
|
schema: ZodRawShape;
|
|
24
|
+
outputSchema?: ZodRawShape;
|
|
18
25
|
default: UserToolHandler;
|
|
19
26
|
};
|
|
20
27
|
|
|
@@ -30,29 +37,22 @@ export type ResourceFile = {
|
|
|
30
37
|
default: UserResourceHandler;
|
|
31
38
|
};
|
|
32
39
|
|
|
33
|
-
// @ts-expect-error: injected by compiler
|
|
34
40
|
export const injectedTools = INJECTED_TOOLS as Record<
|
|
35
41
|
string,
|
|
36
42
|
() => Promise<ToolFile>
|
|
37
43
|
>;
|
|
38
44
|
|
|
39
|
-
// @ts-expect-error: injected by compiler
|
|
40
45
|
export const injectedPrompts = INJECTED_PROMPTS as Record<
|
|
41
46
|
string,
|
|
42
47
|
() => Promise<PromptFile>
|
|
43
48
|
>;
|
|
44
49
|
|
|
45
|
-
// @ts-expect-error: injected by compiler
|
|
46
50
|
export const injectedResources = INJECTED_RESOURCES as Record<
|
|
47
51
|
string,
|
|
48
52
|
() => Promise<ResourceFile>
|
|
49
53
|
>;
|
|
50
54
|
|
|
51
|
-
export const INJECTED_CONFIG =
|
|
52
|
-
// TODO get from project config
|
|
53
|
-
name: "MCP Server",
|
|
54
|
-
version: "0.0.1",
|
|
55
|
-
} as const satisfies Implementation;
|
|
55
|
+
export const INJECTED_CONFIG = SERVER_INFO as Implementation;
|
|
56
56
|
|
|
57
57
|
/* Loads all modules and injects them into the server */
|
|
58
58
|
// would be better as a class and use dependency injection perhaps
|
|
@@ -70,49 +70,37 @@ export async function configureServer(
|
|
|
70
70
|
return server;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
export function loadTools() {
|
|
74
|
-
const toolModules =
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
injectedTools[path]().then((toolModule) => {
|
|
78
|
-
toolModules.set(path, toolModule);
|
|
79
|
-
})
|
|
80
|
-
);
|
|
81
|
-
|
|
82
|
-
return [toolPromises, toolModules] as const;
|
|
73
|
+
export async function loadTools() {
|
|
74
|
+
const { toolModules, skippedTools } = await loadToolModules(injectedTools);
|
|
75
|
+
reportToolLoadIssues(skippedTools);
|
|
76
|
+
return toolModules;
|
|
83
77
|
}
|
|
84
78
|
|
|
85
|
-
export function loadPrompts() {
|
|
86
|
-
const promptModules =
|
|
87
|
-
|
|
88
|
-
const promptPromises = Object.keys(injectedPrompts).map((path) =>
|
|
89
|
-
injectedPrompts[path]().then((promptModule) => {
|
|
90
|
-
promptModules.set(path, promptModule);
|
|
91
|
-
})
|
|
79
|
+
export async function loadPrompts() {
|
|
80
|
+
const { promptModules, skippedPrompts } = await loadPromptModules(
|
|
81
|
+
injectedPrompts
|
|
92
82
|
);
|
|
93
|
-
|
|
94
|
-
return
|
|
83
|
+
reportPromptLoadIssues(skippedPrompts);
|
|
84
|
+
return promptModules;
|
|
95
85
|
}
|
|
96
86
|
|
|
97
|
-
export function loadResources() {
|
|
98
|
-
const resourceModules =
|
|
99
|
-
|
|
100
|
-
const resourcePromises = Object.keys(injectedResources).map((path) =>
|
|
101
|
-
injectedResources[path]().then((resourceModule) => {
|
|
102
|
-
resourceModules.set(path, resourceModule);
|
|
103
|
-
})
|
|
87
|
+
export async function loadResources() {
|
|
88
|
+
const { resourceModules, skippedResources } = await loadResourceModules(
|
|
89
|
+
injectedResources
|
|
104
90
|
);
|
|
105
|
-
|
|
106
|
-
return
|
|
91
|
+
reportResourceLoadIssues(skippedResources);
|
|
92
|
+
return resourceModules;
|
|
107
93
|
}
|
|
108
94
|
|
|
109
95
|
export async function createServer() {
|
|
110
96
|
const server = new McpServer(INJECTED_CONFIG);
|
|
111
|
-
const
|
|
112
|
-
const
|
|
113
|
-
const
|
|
114
|
-
await Promise.all(
|
|
115
|
-
|
|
116
|
-
|
|
97
|
+
const toolModulesPromise = loadTools();
|
|
98
|
+
const promptModulesPromise = loadPrompts();
|
|
99
|
+
const resourceModulesPromise = loadResources();
|
|
100
|
+
const [toolModules, promptModules, resourceModules] = await Promise.all([
|
|
101
|
+
toolModulesPromise,
|
|
102
|
+
promptModulesPromise,
|
|
103
|
+
resourceModulesPromise,
|
|
104
|
+
]);
|
|
117
105
|
return configureServer(server, toolModules, promptModules, resourceModules);
|
|
118
106
|
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { ToolFile } from "./server";
|
|
2
|
+
|
|
3
|
+
const EMPTY_TOOL_FILE_MESSAGE = "File is empty.";
|
|
4
|
+
const MISSING_DEFAULT_EXPORT_MESSAGE =
|
|
5
|
+
"File does not export a default tool handler.";
|
|
6
|
+
const INVALID_DEFAULT_EXPORT_MESSAGE =
|
|
7
|
+
"Default export must be a tool handler function.";
|
|
8
|
+
|
|
9
|
+
type ToolLoadIssue = {
|
|
10
|
+
path: string;
|
|
11
|
+
message: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
function createToolLoadIssue(path: string): ToolLoadIssue {
|
|
15
|
+
return {
|
|
16
|
+
path,
|
|
17
|
+
message: EMPTY_TOOL_FILE_MESSAGE,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function createInvalidToolImplementationError(path: string): Error {
|
|
22
|
+
return new Error(
|
|
23
|
+
`[xmcp] Invalid tool file: ${path}\n -> ${INVALID_DEFAULT_EXPORT_MESSAGE}`
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function classifyToolModule(
|
|
28
|
+
toolModule: unknown,
|
|
29
|
+
path: string
|
|
30
|
+
): "empty" | "missing-default" | "valid" {
|
|
31
|
+
if (typeof toolModule !== "object" || toolModule === null) {
|
|
32
|
+
throw createInvalidToolImplementationError(path);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const moduleRecord = toolModule as Record<string, unknown>;
|
|
36
|
+
const keys = Object.keys(moduleRecord);
|
|
37
|
+
const defaultExport = moduleRecord.default;
|
|
38
|
+
const nonInteropKeys = keys.filter((key) => key !== "__esModule");
|
|
39
|
+
|
|
40
|
+
if (
|
|
41
|
+
keys.length === 0 ||
|
|
42
|
+
(nonInteropKeys.length === 1 &&
|
|
43
|
+
nonInteropKeys[0] === "default" &&
|
|
44
|
+
typeof defaultExport === "object" &&
|
|
45
|
+
defaultExport !== null &&
|
|
46
|
+
Object.keys(defaultExport as Record<string, unknown>).length === 0)
|
|
47
|
+
) {
|
|
48
|
+
return "empty";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!("default" in moduleRecord) || moduleRecord.default === undefined) {
|
|
52
|
+
return "missing-default";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (typeof moduleRecord.default !== "function") {
|
|
56
|
+
throw createInvalidToolImplementationError(path);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return "valid";
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function toToolFile(toolModule: unknown): ToolFile {
|
|
63
|
+
return toolModule as ToolFile;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function loadToolModules(
|
|
67
|
+
loaders: Record<string, () => Promise<unknown>>
|
|
68
|
+
) {
|
|
69
|
+
const toolModules = new Map<string, ToolFile>();
|
|
70
|
+
const skippedTools: ToolLoadIssue[] = [];
|
|
71
|
+
|
|
72
|
+
const results = await Promise.all(
|
|
73
|
+
Object.entries(loaders).map(async ([path, loadTool]) => {
|
|
74
|
+
const toolModule = await loadTool();
|
|
75
|
+
const classification = classifyToolModule(toolModule, path);
|
|
76
|
+
|
|
77
|
+
if (classification === "empty") {
|
|
78
|
+
return {
|
|
79
|
+
skipped: true as const,
|
|
80
|
+
issue: createToolLoadIssue(path),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (classification === "missing-default") {
|
|
85
|
+
return {
|
|
86
|
+
skipped: true as const,
|
|
87
|
+
issue: {
|
|
88
|
+
path,
|
|
89
|
+
message: MISSING_DEFAULT_EXPORT_MESSAGE,
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
skipped: false as const,
|
|
96
|
+
path,
|
|
97
|
+
toolModule: toToolFile(toolModule),
|
|
98
|
+
};
|
|
99
|
+
})
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
for (const result of results) {
|
|
103
|
+
if (result.skipped) {
|
|
104
|
+
skippedTools.push(result.issue);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
toolModules.set(result.path, result.toolModule);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
toolModules,
|
|
113
|
+
skippedTools,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function createToolLoadIssueReporter(
|
|
118
|
+
logger: Pick<Console, "warn"> = console
|
|
119
|
+
) {
|
|
120
|
+
let lastReportedSummaryKey = "";
|
|
121
|
+
|
|
122
|
+
return (skippedTools: ToolLoadIssue[]) => {
|
|
123
|
+
const summaryKey = skippedTools
|
|
124
|
+
.map(({ path, message }) => `${path}:${message}`)
|
|
125
|
+
.sort()
|
|
126
|
+
.join("|");
|
|
127
|
+
|
|
128
|
+
if (summaryKey === lastReportedSummaryKey) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
lastReportedSummaryKey = summaryKey;
|
|
133
|
+
|
|
134
|
+
if (skippedTools.length === 0) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
skippedTools.forEach(({ path, message }) => {
|
|
139
|
+
logger.warn(`[xmcp] Failed to load tool file: ${path}\n -> ${message}`);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const count = skippedTools.length;
|
|
143
|
+
logger.warn(
|
|
144
|
+
`[xmcp] ${count} tool${count === 1 ? "" : "s"} skipped due to empty files or missing default exports`
|
|
145
|
+
);
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export const reportToolLoadIssues = createToolLoadIssueReporter();
|
|
@@ -8,7 +8,7 @@ import { isReactFile } from "./react";
|
|
|
8
8
|
import { uIResourceRegistry } from "./ext-apps-registry";
|
|
9
9
|
import { flattenMeta, hasUIMeta } from "./ui/flatten-meta";
|
|
10
10
|
import { splitUIMetaNested } from "./ui/split-meta";
|
|
11
|
-
import { isPaidHandler, getX402Registry } from "
|
|
11
|
+
import { isPaidHandler, getX402Registry } from "@/plugins/x402";
|
|
12
12
|
|
|
13
13
|
/** Validates if a value is a valid Zod schema object */
|
|
14
14
|
export function isZodRawShape(value: unknown): value is ZodRawShape {
|
|
@@ -30,6 +30,14 @@ export function pathToName(path: string): string {
|
|
|
30
30
|
return fileName.replace(/\.[^/.]+$/, "");
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
/** Ensures toolConfig has its own annotations object with a title */
|
|
34
|
+
export function ensureAnnotations(toolConfig: Pick<ToolMetadata, "name" | "annotations">): void {
|
|
35
|
+
toolConfig.annotations = { ...(toolConfig.annotations ?? {}) };
|
|
36
|
+
if (toolConfig.annotations.title === undefined) {
|
|
37
|
+
toolConfig.annotations.title = toolConfig.name;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
33
41
|
/** Loads tools and injects them into the server */
|
|
34
42
|
export function addToolsToServer(
|
|
35
43
|
server: McpServer,
|
|
@@ -43,7 +51,7 @@ export function addToolsToServer(
|
|
|
43
51
|
description: "No description provided",
|
|
44
52
|
};
|
|
45
53
|
|
|
46
|
-
const { default: handler, metadata, schema } = toolModule;
|
|
54
|
+
const { default: handler, metadata, schema, outputSchema } = toolModule;
|
|
47
55
|
|
|
48
56
|
if (typeof metadata === "object" && metadata !== null) {
|
|
49
57
|
Object.assign(toolConfig, metadata);
|
|
@@ -67,14 +75,20 @@ export function addToolsToServer(
|
|
|
67
75
|
);
|
|
68
76
|
}
|
|
69
77
|
|
|
70
|
-
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
78
|
+
let toolOutputSchema: ZodRawShape | undefined;
|
|
79
|
+
if (outputSchema !== undefined && outputSchema !== null) {
|
|
80
|
+
if (isZodRawShape(outputSchema)) {
|
|
81
|
+
toolOutputSchema = outputSchema;
|
|
82
|
+
} else {
|
|
83
|
+
throw new Error(
|
|
84
|
+
`Invalid outputSchema for tool "${toolConfig.name}" at ${path}. Expected Record<string, z.ZodType>`
|
|
85
|
+
);
|
|
86
|
+
}
|
|
76
87
|
}
|
|
77
88
|
|
|
89
|
+
// Make sure tools has annotations with a title
|
|
90
|
+
ensureAnnotations(toolConfig);
|
|
91
|
+
|
|
78
92
|
if (toolConfig._meta === undefined) {
|
|
79
93
|
toolConfig._meta = {};
|
|
80
94
|
}
|
|
@@ -136,7 +150,12 @@ export function addToolsToServer(
|
|
|
136
150
|
},
|
|
137
151
|
});
|
|
138
152
|
} else {
|
|
139
|
-
transformedHandler = transformToolHandler(
|
|
153
|
+
transformedHandler = transformToolHandler(
|
|
154
|
+
handler,
|
|
155
|
+
meta,
|
|
156
|
+
toolOutputSchema,
|
|
157
|
+
toolConfig.name
|
|
158
|
+
);
|
|
140
159
|
}
|
|
141
160
|
|
|
142
161
|
const toolConfigFormatted = {
|
|
@@ -145,6 +164,9 @@ export function addToolsToServer(
|
|
|
145
164
|
// Build the object schema using the project's Zod instance to avoid
|
|
146
165
|
// cross-instance v3 shape issues in tools/list JSON schema generation.
|
|
147
166
|
inputSchema: z.object(toolSchema),
|
|
167
|
+
outputSchema: toolOutputSchema
|
|
168
|
+
? z.object(toolOutputSchema).strict()
|
|
169
|
+
: undefined,
|
|
148
170
|
annotations: toolConfig.annotations,
|
|
149
171
|
_meta: flattenedToolMeta, // Use flattened metadata for MCP protocol
|
|
150
172
|
};
|