Skip to content

Commit 80a2a98

Browse files
committed
fix(gateway): harden MCP loopback schema projection
1 parent 1e08af4 commit 80a2a98

3 files changed

Lines changed: 353 additions & 15 deletions

File tree

‎src/gateway/mcp-http.handlers.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import {
99
jsonRpcResult,
1010
type JsonRpcRequest,
1111
} from "./mcp-http.protocol.js";
12-
import type { McpLoopbackTool, McpToolSchemaEntry } from "./mcp-http.schema.js";
12+
import {
13+
readMcpLoopbackToolName,
14+
type McpLoopbackTool,
15+
type McpToolSchemaEntry,
16+
} from "./mcp-http.schema.js";
1317

1418
type McpTextContent = {
1519
type: "text";
@@ -62,9 +66,23 @@ export async function handleMcpJsonRpc(params: {
6266
case "tools/list":
6367
return jsonRpcResult(id, { tools: params.toolSchema });
6468
case "tools/call": {
65-
const toolName = methodParams?.name as string;
69+
const toolName = typeof methodParams?.name === "string" ? methodParams.name.trim() : "";
6670
const toolArgs = (methodParams?.arguments ?? {}) as Record<string, unknown>;
67-
const tool = params.tools.find((candidate) => candidate.name === toolName);
71+
if (!toolName) {
72+
return jsonRpcResult(id, {
73+
content: [{ type: "text", text: "Tool not available: unknown" }],
74+
isError: true,
75+
});
76+
}
77+
if (!params.toolSchema.some((tool) => tool.name === toolName)) {
78+
return jsonRpcResult(id, {
79+
content: [{ type: "text", text: `Tool not available: ${toolName}` }],
80+
isError: true,
81+
});
82+
}
83+
const tool = params.tools.find(
84+
(candidate) => readMcpLoopbackToolName(candidate) === toolName,
85+
);
6886
if (!tool) {
6987
return jsonRpcResult(id, {
7088
content: [{ type: "text", text: `Tool not available: ${toolName}` }],

‎src/gateway/mcp-http.schema.ts‎

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isRecord } from "@openclaw/normalization-core/record-coerce";
12
import { uniqueValues } from "@openclaw/normalization-core/string-normalization";
23
import { logWarn } from "../logger.js";
34
import { resolveGatewayScopedTools } from "./tool-resolution.js";
@@ -10,23 +11,92 @@ export type McpToolSchemaEntry = {
1011
inputSchema: Record<string, unknown>;
1112
};
1213

14+
function readLoopbackToolField(tool: McpLoopbackTool, key: "name" | "description" | "parameters") {
15+
try {
16+
return (tool as unknown as Record<typeof key, unknown>)[key];
17+
} catch {
18+
return undefined;
19+
}
20+
}
21+
22+
export function readMcpLoopbackToolName(tool: McpLoopbackTool): string | undefined {
23+
const value = readLoopbackToolField(tool, "name");
24+
if (typeof value !== "string") {
25+
return undefined;
26+
}
27+
const name = value.trim();
28+
return name || undefined;
29+
}
30+
31+
function readLoopbackToolDescription(tool: McpLoopbackTool): string | undefined {
32+
const value = readLoopbackToolField(tool, "description");
33+
return typeof value === "string" ? value : undefined;
34+
}
35+
36+
function readLoopbackToolParameters(tool: McpLoopbackTool): Record<string, unknown> | undefined {
37+
let value;
38+
try {
39+
value = (tool as unknown as { parameters?: unknown }).parameters;
40+
} catch {
41+
return undefined;
42+
}
43+
if (!isRecord(value)) {
44+
return {};
45+
}
46+
try {
47+
return { ...value };
48+
} catch {
49+
return undefined;
50+
}
51+
}
52+
1353
function flattenUnionSchema(raw: Record<string, unknown>): Record<string, unknown> {
14-
const variants = (raw.anyOf ?? raw.oneOf) as Record<string, unknown>[] | undefined;
54+
const variants = (raw.anyOf ?? raw.oneOf) as unknown[] | undefined;
1555
if (!Array.isArray(variants) || variants.length === 0) {
1656
return raw;
1757
}
1858
const mergedProps: Record<string, unknown> = {};
1959
const requiredSets: Set<string>[] = [];
2060
for (const variant of variants) {
21-
const props = variant.properties as Record<string, unknown> | undefined;
61+
if (variant === true) {
62+
requiredSets.push(new Set());
63+
continue;
64+
}
65+
if (!isRecord(variant)) {
66+
continue;
67+
}
68+
const props = isRecord(variant.properties) ? variant.properties : undefined;
2269
if (props) {
2370
for (const [key, schema] of Object.entries(props)) {
71+
if (!isPropertySchema(schema)) {
72+
logWarn(`mcp loopback: malformed schema definition for "${key}", ignoring that variant`);
73+
continue;
74+
}
2475
if (!(key in mergedProps)) {
2576
mergedProps[key] = schema;
2677
continue;
2778
}
28-
const existing = mergedProps[key] as Record<string, unknown>;
29-
const incoming = schema as Record<string, unknown>;
79+
const existing = mergedProps[key];
80+
const incoming = schema;
81+
if (existing === true || incoming === true) {
82+
mergedProps[key] = true;
83+
continue;
84+
}
85+
if (existing === false) {
86+
mergedProps[key] = incoming;
87+
continue;
88+
}
89+
if (incoming === false) {
90+
continue;
91+
}
92+
if (!isRecord(existing) || !isRecord(incoming)) {
93+
if (existing !== incoming) {
94+
logWarn(
95+
`mcp loopback: conflicting schema definitions for "${key}", keeping the first variant`,
96+
);
97+
}
98+
continue;
99+
}
30100
if (Array.isArray(existing.enum) && Array.isArray(incoming.enum)) {
31101
mergedProps[key] = {
32102
...existing,
@@ -54,18 +124,28 @@ function flattenUnionSchema(raw: Record<string, unknown>): Record<string, unknow
54124
}
55125
const required =
56126
requiredSets.length > 0
57-
? [...(requiredSets[0] ?? [])].filter((key) => requiredSets.every((set) => set.has(key)))
127+
? [...(requiredSets[0] ?? [])].filter(
128+
(key) => key in mergedProps && requiredSets.every((set) => set.has(key)),
129+
)
58130
: [];
59131
const { anyOf: _anyOf, oneOf: _oneOf, ...rest } = raw;
60132
return { ...rest, type: "object", properties: mergedProps, required };
61133
}
62134

135+
function isPropertySchema(value: unknown): value is boolean | Record<string, unknown> {
136+
return typeof value === "boolean" || isRecord(value);
137+
}
138+
63139
export function buildMcpToolSchema(tools: McpLoopbackTool[]): McpToolSchemaEntry[] {
64-
return tools.map((tool) => {
65-
let raw =
66-
tool.parameters && typeof tool.parameters === "object"
67-
? { ...(tool.parameters as Record<string, unknown>) }
68-
: {};
140+
return tools.flatMap((tool) => {
141+
const name = readMcpLoopbackToolName(tool);
142+
if (!name) {
143+
return [];
144+
}
145+
let raw = readLoopbackToolParameters(tool);
146+
if (!raw) {
147+
return [];
148+
}
69149
if (raw.anyOf || raw.oneOf) {
70150
raw = flattenUnionSchema(raw);
71151
}
@@ -76,8 +156,8 @@ export function buildMcpToolSchema(tools: McpLoopbackTool[]): McpToolSchemaEntry
76156
raw.properties = {};
77157
}
78158
return {
79-
name: tool.name,
80-
description: tool.description,
159+
name,
160+
description: readLoopbackToolDescription(tool),
81161
inputSchema: raw,
82162
};
83163
});

0 commit comments

Comments
 (0)