Improve MCP structured result visibility (#167)

This commit is contained in:
Ben Senescu 2026-05-08 16:17:28 -04:00 committed by GitHub
parent 2234cb54fc
commit f58efea164
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 56 additions and 5 deletions

View File

@ -44,4 +44,41 @@ describe("mcpResponse", () => {
}); });
expect(result.structuredContent).toEqual({ foo: "bar" }); expect(result.structuredContent).toEqual({ foo: "bar" });
}); });
it("mirrors metadata into structuredContent for clients that hide _meta", () => {
const result = mcpResponse({
text: "hi",
meta: {
url: "https://app.openseo.so/p/1",
projectId: "1",
creditsRemaining: 100,
},
structuredContent: { foo: "bar" },
});
expect(result.structuredContent).toEqual({
foo: "bar",
meta: {
url: "https://app.openseo.so/p/1",
projectId: "1",
creditsRemaining: 100,
},
});
expect(result._meta).toEqual({
url: "https://app.openseo.so/p/1",
projectId: "1",
creditsRemaining: 100,
});
});
it("uses metadata as structuredContent when no data payload is provided", () => {
const result = mcpResponse({
text: "hi",
meta: { url: "https://app.openseo.so" },
});
expect(result.structuredContent).toEqual({
meta: { url: "https://app.openseo.so" },
});
});
}); });

View File

@ -17,15 +17,22 @@ export function mcpResponse(opts: {
const result: CallToolResult = { const result: CallToolResult = {
content: [{ type: "text", text: opts.text }], content: [{ type: "text", text: opts.text }],
}; };
if (opts.structuredContent) { let meta: Record<string, unknown> | undefined;
result.structuredContent = opts.structuredContent;
}
if (opts.meta) { if (opts.meta) {
// Drop undefined keys so the wire payload stays clean. meta = {};
const meta: Record<string, unknown> = {};
for (const [key, value] of Object.entries(opts.meta)) { for (const [key, value] of Object.entries(opts.meta)) {
if (value !== undefined) meta[key] = value; if (value !== undefined) meta[key] = value;
} }
}
if (opts.structuredContent) {
result.structuredContent =
meta && Object.keys(meta).length > 0
? { ...opts.structuredContent, meta }
: opts.structuredContent;
} else if (meta && Object.keys(meta).length > 0) {
result.structuredContent = { meta };
}
if (meta) {
if (Object.keys(meta).length > 0) { if (Object.keys(meta).length > 0) {
result._meta = meta; result._meta = meta;
} }

View File

@ -46,6 +46,13 @@ export const saveKeywordsTool = {
args.projectId, args.projectId,
`/p/${args.projectId}/saved`, `/p/${args.projectId}/saved`,
), ),
structuredContent: {
projectId: args.projectId,
savedCount: args.keywords.length,
keywords: args.keywords,
locationCode: args.locationCode ?? DEFAULT_LOCATION_CODE,
languageCode: args.languageCode ?? DEFAULT_LANGUAGE_CODE,
},
}); });
}), }),
}; };