xmcp 0.6.3 → 0.6.5
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 +11 -15
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/runtime/adapter-express.js +10 -33
- package/dist/runtime/adapter-nestjs.js +15 -38
- package/dist/runtime/adapter-nextjs.js +10 -33
- package/dist/runtime/http.js +19 -42
- package/dist/runtime/stdio.js +11 -34
- package/dist/types/resource.d.ts +3 -3
- package/dist/types/tool.d.ts +3 -4
- package/dist/types/ui-meta.d.ts +22 -0
- package/package.json +2 -2
- package/src/compiler/client/component-compiler.ts +0 -4
- package/src/compiler/index.ts +1 -1
- package/src/compiler/on-first-build.ts +34 -50
- package/src/index.ts +1 -1
- package/src/runtime/utils/react/generate-html.ts +0 -32
- package/src/runtime/utils/resources.ts +15 -15
- package/src/runtime/utils/server.ts +2 -3
- package/src/runtime/utils/tools.ts +33 -68
- package/src/runtime/utils/transformers/tool.ts +21 -21
- package/src/types/resource.ts +3 -3
- package/src/types/tool.ts +3 -4
- package/src/types/ui-meta.ts +26 -0
- package/dist/types/openai-meta.d.ts +0 -173
- package/src/runtime/utils/openai/flatten-meta.ts +0 -44
- package/src/runtime/utils/openai/split-meta.ts +0 -81
- package/src/runtime/utils/openai-resource-registry.ts +0 -58
- package/src/types/openai-meta.ts +0 -191
package/dist/types/resource.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UIMetadata } from "./ui-meta";
|
|
2
2
|
export interface ResourceMetadata {
|
|
3
3
|
name: string;
|
|
4
4
|
title?: string;
|
|
5
5
|
description?: string;
|
|
6
6
|
mimeType?: string;
|
|
7
7
|
size?: number;
|
|
8
|
-
/** Metadata for the resource.
|
|
8
|
+
/** Metadata for the resource. */
|
|
9
9
|
_meta?: {
|
|
10
|
-
|
|
10
|
+
ui?: UIMetadata;
|
|
11
11
|
[key: string]: unknown;
|
|
12
12
|
};
|
|
13
13
|
[key: string]: any;
|
package/dist/types/tool.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { z } from "zod/v3";
|
|
2
2
|
import type { ZodType as ZodTypeV4, infer as inferV4 } from "zod";
|
|
3
|
-
import {
|
|
3
|
+
import { UIMetadata } from "./ui-meta";
|
|
4
4
|
export interface ToolAnnotations {
|
|
5
5
|
/** Human-readable title for the tool */
|
|
6
6
|
title?: string;
|
|
@@ -21,10 +21,9 @@ export interface ToolMetadata {
|
|
|
21
21
|
description: string;
|
|
22
22
|
/** Optional hints about tool behavior */
|
|
23
23
|
annotations?: ToolAnnotations;
|
|
24
|
-
/** Metadata for the tool.
|
|
24
|
+
/** Metadata for the tool. */
|
|
25
25
|
_meta?: {
|
|
26
|
-
|
|
27
|
-
ui?: OpenAIMetadata;
|
|
26
|
+
ui?: UIMetadata;
|
|
28
27
|
[key: string]: unknown;
|
|
29
28
|
};
|
|
30
29
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface UIMetadata {
|
|
2
|
+
/**
|
|
3
|
+
* URI template for the widget resource.
|
|
4
|
+
* Auto-generated as `ui://app/{toolName}.html` when omitted.
|
|
5
|
+
*/
|
|
6
|
+
resourceUri?: string;
|
|
7
|
+
/**
|
|
8
|
+
* Content Security Policy configuration for widget resources.
|
|
9
|
+
*/
|
|
10
|
+
csp?: {
|
|
11
|
+
connectDomains?: string[];
|
|
12
|
+
resourceDomains?: string[];
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Optional dedicated origin for this widget.
|
|
16
|
+
*/
|
|
17
|
+
domain?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Visual hint indicating the widget prefers a border.
|
|
20
|
+
*/
|
|
21
|
+
prefersBorder?: boolean;
|
|
22
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xmcp",
|
|
3
3
|
"description": "The MCP framework",
|
|
4
|
-
"version": "0.6.
|
|
4
|
+
"version": "0.6.5",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "xmcp",
|
|
7
7
|
"email": "support@xmcp.dev",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"test:config": "node --import tsx --test src/compiler/config/__tests__/*.test.ts"
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
|
-
"@modelcontextprotocol/sdk": "^1.
|
|
71
|
+
"@modelcontextprotocol/sdk": "^1.26.0",
|
|
72
72
|
"@rspack/core": "^1.6.7",
|
|
73
73
|
"jose": "^6.1.3",
|
|
74
74
|
"postcss-loader": "^8.2.0",
|
|
@@ -125,10 +125,6 @@ export class ClientComponentCompiler {
|
|
|
125
125
|
root.render(React.createElement(Component, props));
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
if (window.openai !== undefined) {
|
|
129
|
-
render(window.openai.toolInput ?? {});
|
|
130
|
-
}
|
|
131
|
-
|
|
132
128
|
window.addEventListener("message", (event) => {
|
|
133
129
|
if (event.source !== window.parent && event.source !== null) return;
|
|
134
130
|
|
package/src/compiler/index.ts
CHANGED
|
@@ -269,7 +269,7 @@ export async function compile({ onBuild }: CompileOptions = {}) {
|
|
|
269
269
|
|
|
270
270
|
// Build succeeded
|
|
271
271
|
if (firstBuild) {
|
|
272
|
-
onFirstBuild(
|
|
272
|
+
onFirstBuild(xmcpConfig);
|
|
273
273
|
} else {
|
|
274
274
|
// on dev mode, bundler will recompile the code, so we need to start the http server after the first one
|
|
275
275
|
if (
|
|
@@ -1,56 +1,9 @@
|
|
|
1
|
-
import { spawn } from "child_process";
|
|
2
|
-
import { CompilerMode } from ".";
|
|
3
|
-
import { watchdog } from "../utils/spawn-process";
|
|
4
1
|
import { greenCheck } from "../utils/cli-icons";
|
|
5
2
|
import { XmcpConfigOutputSchema } from "./config";
|
|
3
|
+
import { compilerContext } from "./compiler-context";
|
|
4
|
+
import chalk from "chalk";
|
|
6
5
|
|
|
7
|
-
export function onFirstBuild(
|
|
8
|
-
mode: CompilerMode,
|
|
9
|
-
xmcpConfig: XmcpConfigOutputSchema
|
|
10
|
-
) {
|
|
11
|
-
if (mode === "development" && false) {
|
|
12
|
-
// disable inspector for now
|
|
13
|
-
console.log("🔍 Starting inspector...");
|
|
14
|
-
|
|
15
|
-
const inspectorArgs = ["@modelcontextprotocol/inspector@latest"];
|
|
16
|
-
|
|
17
|
-
if (xmcpConfig.stdio) {
|
|
18
|
-
inspectorArgs.push("node", "dist/stdio.js");
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const inspectorProcess = spawn("npx", inspectorArgs, {
|
|
22
|
-
stdio: ["inherit", "pipe", "pipe"],
|
|
23
|
-
shell: true,
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
watchdog(inspectorProcess);
|
|
27
|
-
|
|
28
|
-
// Prefix inspector output with [Inspector]
|
|
29
|
-
inspectorProcess.stdout?.on("data", (data: Buffer) => {
|
|
30
|
-
const lines = data.toString().split("\n");
|
|
31
|
-
lines.forEach((line) => {
|
|
32
|
-
if (line.trim()) {
|
|
33
|
-
if (line.includes("?MCP_PROXY_AUTH_TOKEN")) {
|
|
34
|
-
console.log(`🔍 Inspector started at ${line}`);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
inspectorProcess.stderr?.on("data", (data: Buffer) => {
|
|
41
|
-
const lines = data.toString().split("\n");
|
|
42
|
-
lines.forEach((line) => {
|
|
43
|
-
if (line.trim()) {
|
|
44
|
-
console.error(`[Inspector] ${line}`);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
inspectorProcess.on("error", (err: Error) => {
|
|
50
|
-
console.error("[Inspector] Failed to start inspector:", err);
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
|
|
6
|
+
export function onFirstBuild(xmcpConfig: XmcpConfigOutputSchema) {
|
|
54
7
|
const builtResults = [];
|
|
55
8
|
|
|
56
9
|
if (xmcpConfig.stdio) {
|
|
@@ -67,4 +20,35 @@ export function onFirstBuild(
|
|
|
67
20
|
builtResults.forEach((result) => {
|
|
68
21
|
console.log(result);
|
|
69
22
|
});
|
|
23
|
+
|
|
24
|
+
logBuildSummary();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function logBuildSummary() {
|
|
28
|
+
const { toolPaths, promptPaths, resourcePaths } =
|
|
29
|
+
compilerContext.getContext();
|
|
30
|
+
const toolsCount = toolPaths.size;
|
|
31
|
+
const promptsCount = promptPaths.size;
|
|
32
|
+
const resourcesCount = resourcePaths.size;
|
|
33
|
+
|
|
34
|
+
const parts: string[] = [];
|
|
35
|
+
if (toolsCount > 0) {
|
|
36
|
+
parts.push(`${chalk.bold(toolsCount)} tool${toolsCount !== 1 ? "s" : ""}`);
|
|
37
|
+
}
|
|
38
|
+
if (promptsCount > 0) {
|
|
39
|
+
parts.push(
|
|
40
|
+
`${chalk.bold(promptsCount)} prompt${promptsCount !== 1 ? "s" : ""}`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
if (resourcesCount > 0) {
|
|
44
|
+
parts.push(
|
|
45
|
+
`${chalk.bold(resourcesCount)} resource${resourcesCount !== 1 ? "s" : ""}`
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (parts.length > 0) {
|
|
50
|
+
console.log(`${greenCheck} Registered ${parts.join(", ")}`);
|
|
51
|
+
} else {
|
|
52
|
+
console.log(`${greenCheck} No tools, prompts, or resources registered`);
|
|
53
|
+
}
|
|
70
54
|
}
|
package/src/index.ts
CHANGED
|
@@ -10,7 +10,7 @@ export type {
|
|
|
10
10
|
} from "./types/tool";
|
|
11
11
|
export type { PromptMetadata } from "./types/prompt";
|
|
12
12
|
export type { ResourceMetadata } from "./types/resource";
|
|
13
|
-
export type {
|
|
13
|
+
export type { UIMetadata } from "./types/ui-meta";
|
|
14
14
|
|
|
15
15
|
export type { XmcpConfigInputSchema as XmcpConfig } from "./compiler/config";
|
|
16
16
|
import "./types/declarations";
|
|
@@ -1,35 +1,3 @@
|
|
|
1
|
-
export function generateOpenAIHTML(
|
|
2
|
-
componentCode: string,
|
|
3
|
-
css: string | undefined
|
|
4
|
-
): string {
|
|
5
|
-
const styleTag = css ? `<style>${css}</style>` : "";
|
|
6
|
-
|
|
7
|
-
return `<!DOCTYPE html>
|
|
8
|
-
<html>
|
|
9
|
-
<head>
|
|
10
|
-
<meta charset="utf-8" />
|
|
11
|
-
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
12
|
-
${styleTag}
|
|
13
|
-
</head>
|
|
14
|
-
<body>
|
|
15
|
-
<div id="root"></div>
|
|
16
|
-
|
|
17
|
-
<script type="module">
|
|
18
|
-
const componentSource = ${JSON.stringify(componentCode)};
|
|
19
|
-
const blobUrl = URL.createObjectURL(
|
|
20
|
-
new Blob([componentSource], { type: "text/javascript" })
|
|
21
|
-
);
|
|
22
|
-
|
|
23
|
-
await import(blobUrl);
|
|
24
|
-
|
|
25
|
-
requestAnimationFrame(() => {
|
|
26
|
-
URL.revokeObjectURL(blobUrl);
|
|
27
|
-
});
|
|
28
|
-
</script>
|
|
29
|
-
</body>
|
|
30
|
-
</html>`;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
1
|
export function generateUIHTML(
|
|
34
2
|
componentCode: string,
|
|
35
3
|
css: string | undefined
|
|
@@ -9,9 +9,8 @@ import { ZodRawShape } from "zod/v3";
|
|
|
9
9
|
import { transformResourceHandler } from "./transformers/resource";
|
|
10
10
|
import { composeUriFromPath } from "./utils/resource-uri-composer";
|
|
11
11
|
import { ResourceMetadata } from "@/types/resource";
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import { generateOpenAIHTML, generateUIHTML } from "./react/generate-html";
|
|
12
|
+
import { flattenMeta } from "./ui/flatten-meta";
|
|
13
|
+
import { generateUIHTML } from "./react/generate-html";
|
|
15
14
|
import { pathToToolNameMd5, pathToToolNameDjb2 } from "./path-to-tool-name";
|
|
16
15
|
import { uIResourceRegistry } from "./ext-apps-registry";
|
|
17
16
|
|
|
@@ -90,13 +89,11 @@ export function addResourcesToServer(
|
|
|
90
89
|
server: McpServer,
|
|
91
90
|
resourcesModules: Map<string, ResourceFile>
|
|
92
91
|
): McpServer {
|
|
93
|
-
|
|
94
|
-
const autoGeneratedResources = [
|
|
95
|
-
...openAIResourceRegistry.getAll(),
|
|
96
|
-
...uIResourceRegistry.getAll(),
|
|
97
|
-
];
|
|
92
|
+
const autoGeneratedResources = [...uIResourceRegistry.getAll()];
|
|
98
93
|
|
|
99
94
|
autoGeneratedResources.forEach(([_, autoResource]) => {
|
|
95
|
+
const resourceMimeType =
|
|
96
|
+
autoResource.mimeType || "text/html;profile=mcp-app";
|
|
100
97
|
// Flatten resource metadata for MCP protocol
|
|
101
98
|
const flattenedResourceMeta = autoResource?._meta
|
|
102
99
|
? flattenMeta(autoResource._meta)
|
|
@@ -129,18 +126,14 @@ export function addResourcesToServer(
|
|
|
129
126
|
const clientCode = bundle.js;
|
|
130
127
|
const clientCss = bundle.css;
|
|
131
128
|
|
|
132
|
-
const
|
|
133
|
-
autoResource.mimeType === "text/html;profile=mcp-app";
|
|
134
|
-
const fullHTML = isMCPApp
|
|
135
|
-
? generateUIHTML(clientCode, clientCss)
|
|
136
|
-
: generateOpenAIHTML(clientCode, clientCss);
|
|
129
|
+
const fullHTML = generateUIHTML(clientCode, clientCss);
|
|
137
130
|
|
|
138
131
|
return {
|
|
139
132
|
contents: [
|
|
140
133
|
{
|
|
141
134
|
uri: autoResource.uri,
|
|
142
135
|
text: fullHTML,
|
|
143
|
-
mimeType:
|
|
136
|
+
mimeType: resourceMimeType,
|
|
144
137
|
_meta: autoResource._meta,
|
|
145
138
|
},
|
|
146
139
|
],
|
|
@@ -165,13 +158,14 @@ export function addResourcesToServer(
|
|
|
165
158
|
{
|
|
166
159
|
uri: autoResource.uri,
|
|
167
160
|
text: response,
|
|
161
|
+
mimeType: resourceMimeType,
|
|
162
|
+
_meta: autoResource._meta,
|
|
168
163
|
},
|
|
169
164
|
],
|
|
170
165
|
};
|
|
171
166
|
}
|
|
172
167
|
|
|
173
168
|
// If the tool returns { _meta: ... }, return empty HTML
|
|
174
|
-
// (OpenAI handles the rendering)
|
|
175
169
|
if (
|
|
176
170
|
response &&
|
|
177
171
|
typeof response === "object" &&
|
|
@@ -183,6 +177,8 @@ export function addResourcesToServer(
|
|
|
183
177
|
{
|
|
184
178
|
uri: autoResource.uri,
|
|
185
179
|
text: "",
|
|
180
|
+
mimeType: resourceMimeType,
|
|
181
|
+
_meta: autoResource._meta,
|
|
186
182
|
},
|
|
187
183
|
],
|
|
188
184
|
};
|
|
@@ -208,6 +204,8 @@ export function addResourcesToServer(
|
|
|
208
204
|
{
|
|
209
205
|
uri: autoResource.uri,
|
|
210
206
|
text: textContent.text,
|
|
207
|
+
mimeType: resourceMimeType,
|
|
208
|
+
_meta: autoResource._meta,
|
|
211
209
|
},
|
|
212
210
|
],
|
|
213
211
|
};
|
|
@@ -220,6 +218,8 @@ export function addResourcesToServer(
|
|
|
220
218
|
{
|
|
221
219
|
uri: autoResource.uri,
|
|
222
220
|
text: "",
|
|
221
|
+
mimeType: resourceMimeType,
|
|
222
|
+
_meta: autoResource._meta,
|
|
223
223
|
},
|
|
224
224
|
],
|
|
225
225
|
};
|
|
@@ -10,7 +10,7 @@ import { UserResourceHandler } from "./transformers/resource";
|
|
|
10
10
|
import { ZodRawShape } from "zod/v3";
|
|
11
11
|
import { addResourcesToServer } from "./resources";
|
|
12
12
|
import { ResourceMetadata } from "@/types/resource";
|
|
13
|
-
import {
|
|
13
|
+
import { uIResourceRegistry } from "./ext-apps-registry";
|
|
14
14
|
|
|
15
15
|
export type ToolFile = {
|
|
16
16
|
metadata: ToolMetadata;
|
|
@@ -62,8 +62,7 @@ export async function configureServer(
|
|
|
62
62
|
promptModules: Map<string, PromptFile>,
|
|
63
63
|
resourceModules: Map<string, ResourceFile>
|
|
64
64
|
): Promise<McpServer> {
|
|
65
|
-
|
|
66
|
-
openAIResourceRegistry.clear();
|
|
65
|
+
uIResourceRegistry.clear();
|
|
67
66
|
|
|
68
67
|
addToolsToServer(server, toolModules);
|
|
69
68
|
addPromptsToServer(server, promptModules);
|
|
@@ -4,12 +4,9 @@ import { ZodRawShape } from "zod/v3";
|
|
|
4
4
|
import { ToolFile } from "./server";
|
|
5
5
|
import { ToolMetadata } from "@/types/tool";
|
|
6
6
|
import { transformToolHandler } from "./transformers/tool";
|
|
7
|
-
import { openAIResourceRegistry } from "./openai-resource-registry";
|
|
8
|
-
import { flattenMeta, hasOpenAIMeta } from "./openai/flatten-meta";
|
|
9
7
|
import { isReactFile } from "./react";
|
|
10
|
-
import { splitOpenAIMetaNested } from "./openai/split-meta";
|
|
11
8
|
import { uIResourceRegistry } from "./ext-apps-registry";
|
|
12
|
-
import { hasUIMeta } from "./ui/flatten-meta";
|
|
9
|
+
import { flattenMeta, hasUIMeta } from "./ui/flatten-meta";
|
|
13
10
|
import { splitUIMetaNested } from "./ui/split-meta";
|
|
14
11
|
import { isPaidHandler, getX402Registry } from "xmcp/plugins/x402";
|
|
15
12
|
|
|
@@ -84,85 +81,53 @@ export function addToolsToServer(
|
|
|
84
81
|
|
|
85
82
|
const isReact = isReactFile(path);
|
|
86
83
|
|
|
87
|
-
|
|
88
|
-
const openaiWidget = hasOpenAIMeta(toolConfig._meta);
|
|
89
|
-
const uiWidget = hasUIMeta(toolConfig._meta) || (isReact && !openaiWidget);
|
|
84
|
+
const uiWidget = hasUIMeta(toolConfig._meta) || isReact;
|
|
90
85
|
|
|
91
86
|
let toolSpecificMeta = toolConfig._meta;
|
|
92
87
|
|
|
93
|
-
if (uiWidget
|
|
94
|
-
const openaiResourceUri = `ui://widget/${toolConfig.name}.html`;
|
|
88
|
+
if (uiWidget) {
|
|
95
89
|
const mcpuiResourceUri = `ui://app/${toolConfig.name}.html`;
|
|
96
90
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (!toolConfig._meta.openai) {
|
|
100
|
-
toolConfig._meta.openai = {};
|
|
101
|
-
}
|
|
102
|
-
if (!toolConfig._meta.openai.outputTemplate) {
|
|
103
|
-
toolConfig._meta.openai.outputTemplate = openaiResourceUri;
|
|
104
|
-
}
|
|
91
|
+
if (!toolConfig._meta.ui) {
|
|
92
|
+
toolConfig._meta.ui = {};
|
|
105
93
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (!toolConfig._meta.ui) {
|
|
109
|
-
toolConfig._meta.ui = {};
|
|
110
|
-
}
|
|
111
|
-
if (!toolConfig._meta.ui.resourceUri) {
|
|
112
|
-
toolConfig._meta.ui.resourceUri = mcpuiResourceUri;
|
|
113
|
-
}
|
|
94
|
+
if (!toolConfig._meta.ui.resourceUri) {
|
|
95
|
+
toolConfig._meta.ui.resourceUri = mcpuiResourceUri;
|
|
114
96
|
}
|
|
115
97
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
98
|
+
const uiSplit = splitUIMetaNested(toolSpecificMeta);
|
|
99
|
+
toolSpecificMeta = uiSplit.toolMeta;
|
|
100
|
+
const resourceSpecificMeta = uiSplit.resourceMeta;
|
|
101
|
+
|
|
102
|
+
// Ensure CSP resource domains includes esm.sh
|
|
103
|
+
resourceSpecificMeta.ui = resourceSpecificMeta.ui || {};
|
|
104
|
+
resourceSpecificMeta.ui.csp = resourceSpecificMeta.ui.csp || {};
|
|
105
|
+
resourceSpecificMeta.ui.csp.resourceDomains =
|
|
106
|
+
resourceSpecificMeta.ui.csp.resourceDomains || [];
|
|
107
|
+
|
|
108
|
+
if (
|
|
109
|
+
!resourceSpecificMeta.ui.csp.resourceDomains.includes(
|
|
110
|
+
"https://esm.sh"
|
|
111
|
+
)
|
|
112
|
+
) {
|
|
113
|
+
resourceSpecificMeta.ui.csp.resourceDomains.push("https://esm.sh");
|
|
129
114
|
}
|
|
130
115
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
resourceSpecificMeta.ui.csp.resourceDomains =
|
|
140
|
-
resourceSpecificMeta.ui.csp.resourceDomains || [];
|
|
141
|
-
|
|
142
|
-
if (
|
|
143
|
-
!resourceSpecificMeta.ui.csp.resourceDomains.includes(
|
|
144
|
-
"https://esm.sh"
|
|
145
|
-
)
|
|
146
|
-
) {
|
|
147
|
-
resourceSpecificMeta.ui.csp.resourceDomains.push("https://esm.sh");
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
uIResourceRegistry.add(toolConfig.name, {
|
|
151
|
-
name: toolConfig.name,
|
|
152
|
-
uri: mcpuiResourceUri,
|
|
153
|
-
handler,
|
|
154
|
-
mimeType: "text/html;profile=mcp-app",
|
|
155
|
-
_meta: resourceSpecificMeta,
|
|
156
|
-
toolPath: isReact ? path : undefined,
|
|
157
|
-
});
|
|
158
|
-
}
|
|
116
|
+
uIResourceRegistry.add(toolConfig.name, {
|
|
117
|
+
name: toolConfig.name,
|
|
118
|
+
uri: mcpuiResourceUri,
|
|
119
|
+
handler,
|
|
120
|
+
mimeType: "text/html;profile=mcp-app",
|
|
121
|
+
_meta: resourceSpecificMeta,
|
|
122
|
+
toolPath: isReact ? path : undefined,
|
|
123
|
+
});
|
|
159
124
|
}
|
|
160
125
|
|
|
161
126
|
const flattenedToolMeta = flattenMeta(toolSpecificMeta);
|
|
162
|
-
const meta =
|
|
127
|
+
const meta = uiWidget ? flattenedToolMeta : undefined;
|
|
163
128
|
let transformedHandler;
|
|
164
129
|
|
|
165
|
-
if (isReactFile(path) &&
|
|
130
|
+
if (isReactFile(path) && uiWidget) {
|
|
166
131
|
transformedHandler = async (args: any, extra: any) => ({
|
|
167
132
|
content: [{ type: "text", text: "" }],
|
|
168
133
|
_meta: meta,
|
|
@@ -27,6 +27,14 @@ export type McpToolHandler = (
|
|
|
27
27
|
extra: RequestHandlerExtra<ServerRequest, ServerNotification>
|
|
28
28
|
) => CallToolResult | Promise<CallToolResult>;
|
|
29
29
|
|
|
30
|
+
function hasUIMeta(meta?: Record<string, any>): boolean {
|
|
31
|
+
return !!(
|
|
32
|
+
meta &&
|
|
33
|
+
typeof meta === "object" &&
|
|
34
|
+
Object.keys(meta).some((key) => key.startsWith("ui/"))
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
30
38
|
/**
|
|
31
39
|
* Transforms a user's tool handler into an MCP-compatible handler.
|
|
32
40
|
*
|
|
@@ -34,12 +42,12 @@ export type McpToolHandler = (
|
|
|
34
42
|
* 1. Passes through both args and extra parameters to the user's handler
|
|
35
43
|
* 2. Transforms string/number responses into the required CallToolResult format
|
|
36
44
|
* 3. Allows returning `content`, `structuredContent`, or both
|
|
37
|
-
* 4. Allows returning only `_meta` (without `content`) when it contains
|
|
38
|
-
* 5. Auto-wraps HTML strings with
|
|
45
|
+
* 4. Allows returning only `_meta` (without `content`) when it contains widget metadata
|
|
46
|
+
* 5. Auto-wraps HTML strings with widget metadata if provided
|
|
39
47
|
* 6. Validates that the response is a proper CallToolResult and throws a descriptive error if not
|
|
40
48
|
*
|
|
41
49
|
* @param handler - The user's tool handler function
|
|
42
|
-
* @param meta - Optional metadata to attach to responses (for
|
|
50
|
+
* @param meta - Optional metadata to attach to responses (for MCP app widgets)
|
|
43
51
|
* @returns A transformed handler compatible with McpServer.registerTool
|
|
44
52
|
* @throws Error if the handler returns an invalid response type
|
|
45
53
|
*/
|
|
@@ -59,14 +67,11 @@ export function transformToolHandler(
|
|
|
59
67
|
}
|
|
60
68
|
|
|
61
69
|
if (typeof response === "string" || typeof response === "number") {
|
|
62
|
-
// Check if we have
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (hasOpenAIMeta) {
|
|
69
|
-
// For OpenAI widgets, return empty text content with metadata
|
|
70
|
+
// Check if we have widget metadata to attach
|
|
71
|
+
const hasWidgetMeta = hasUIMeta(meta);
|
|
72
|
+
|
|
73
|
+
if (hasWidgetMeta) {
|
|
74
|
+
// For widget tools, return empty text content with metadata
|
|
70
75
|
// The actual HTML is served by the auto-generated resource
|
|
71
76
|
return {
|
|
72
77
|
content: [
|
|
@@ -93,7 +98,7 @@ export function transformToolHandler(
|
|
|
93
98
|
};
|
|
94
99
|
}
|
|
95
100
|
|
|
96
|
-
// Check if response has _meta but no content (special case for
|
|
101
|
+
// Check if response has _meta but no content (special case for widget metadata)
|
|
97
102
|
if (
|
|
98
103
|
response &&
|
|
99
104
|
typeof response === "object" &&
|
|
@@ -103,12 +108,7 @@ export function transformToolHandler(
|
|
|
103
108
|
) {
|
|
104
109
|
const meta = (response as any)._meta;
|
|
105
110
|
|
|
106
|
-
|
|
107
|
-
if (
|
|
108
|
-
meta &&
|
|
109
|
-
typeof meta === "object" &&
|
|
110
|
-
Object.keys(meta).some((key) => key.startsWith("openai/"))
|
|
111
|
-
) {
|
|
111
|
+
if (hasUIMeta(meta)) {
|
|
112
112
|
// Transform to include empty text content with the _meta
|
|
113
113
|
return {
|
|
114
114
|
content: [
|
|
@@ -156,16 +156,16 @@ export function transformToolHandler(
|
|
|
156
156
|
` content: [{ type: "text", text: "fallback" }],\n` +
|
|
157
157
|
` structuredContent: { your: "data" }\n` +
|
|
158
158
|
`}\n\n` +
|
|
159
|
-
`Or for
|
|
159
|
+
`Or for widget metadata only:\n` +
|
|
160
160
|
`{\n` +
|
|
161
161
|
` _meta: {\n` +
|
|
162
|
-
` "
|
|
162
|
+
` "ui/...": ...\n` +
|
|
163
163
|
` }\n` +
|
|
164
164
|
`}`
|
|
165
165
|
);
|
|
166
166
|
}
|
|
167
167
|
|
|
168
|
-
// Check if response has at least one of: content
|
|
168
|
+
// Check if response has at least one of: content or structuredContent
|
|
169
169
|
const hasContent = "content" in response && Array.isArray(response.content);
|
|
170
170
|
const hasStructuredContent = "structuredContent" in response;
|
|
171
171
|
|
package/src/types/resource.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UIMetadata } from "./ui-meta";
|
|
2
2
|
|
|
3
3
|
export interface ResourceMetadata {
|
|
4
4
|
name: string;
|
|
@@ -6,9 +6,9 @@ export interface ResourceMetadata {
|
|
|
6
6
|
description?: string;
|
|
7
7
|
mimeType?: string;
|
|
8
8
|
size?: number;
|
|
9
|
-
/** Metadata for the resource.
|
|
9
|
+
/** Metadata for the resource. */
|
|
10
10
|
_meta?: {
|
|
11
|
-
|
|
11
|
+
ui?: UIMetadata;
|
|
12
12
|
[key: string]: unknown;
|
|
13
13
|
};
|
|
14
14
|
[key: string]: any;
|
package/src/types/tool.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { z } from "zod/v3";
|
|
2
2
|
import type { ZodType as ZodTypeV4, infer as inferV4 } from "zod";
|
|
3
|
-
import {
|
|
3
|
+
import { UIMetadata } from "./ui-meta";
|
|
4
4
|
|
|
5
5
|
export interface ToolAnnotations {
|
|
6
6
|
/** Human-readable title for the tool */
|
|
@@ -23,10 +23,9 @@ export interface ToolMetadata {
|
|
|
23
23
|
description: string;
|
|
24
24
|
/** Optional hints about tool behavior */
|
|
25
25
|
annotations?: ToolAnnotations;
|
|
26
|
-
/** Metadata for the tool.
|
|
26
|
+
/** Metadata for the tool. */
|
|
27
27
|
_meta?: {
|
|
28
|
-
|
|
29
|
-
ui?: OpenAIMetadata;
|
|
28
|
+
ui?: UIMetadata;
|
|
30
29
|
[key: string]: unknown;
|
|
31
30
|
};
|
|
32
31
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface UIMetadata {
|
|
2
|
+
/**
|
|
3
|
+
* URI template for the widget resource.
|
|
4
|
+
* Auto-generated as `ui://app/{toolName}.html` when omitted.
|
|
5
|
+
*/
|
|
6
|
+
resourceUri?: string;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Content Security Policy configuration for widget resources.
|
|
10
|
+
*/
|
|
11
|
+
csp?: {
|
|
12
|
+
connectDomains?: string[];
|
|
13
|
+
resourceDomains?: string[];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Optional dedicated origin for this widget.
|
|
18
|
+
*/
|
|
19
|
+
domain?: string;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Visual hint indicating the widget prefers a border.
|
|
23
|
+
*/
|
|
24
|
+
prefersBorder?: boolean;
|
|
25
|
+
}
|
|
26
|
+
|