Use content instead of structuredContent in examples
Not all MCP clients support structuredContent in MCP tool results, so
examples now return data as JSON in content and parse it client-side.
Pattern used:
- Server: content: [{ type: "text", text: JSON.stringify(data) }]
- Client: JSON.parse(result.content!.filter(...).map(...).join(""))
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
45259c584b
commit
61cd4ed219
2 changed files with 65 additions and 150 deletions
|
|
@ -5,7 +5,6 @@ import cors from "cors";
|
|||
import express, { type Request, type Response } from "express";
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { z } from "zod";
|
||||
import { RESOURCE_URI_META_KEY } from "../../dist/src/app";
|
||||
|
||||
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3001;
|
||||
|
|
@ -30,14 +29,12 @@ const server = new McpServer({
|
|||
title: "Get Time",
|
||||
description: "Returns the current server time as an ISO 8601 string.",
|
||||
inputSchema: {},
|
||||
outputSchema: { time: z.string() },
|
||||
_meta: { [RESOURCE_URI_META_KEY]: resourceUri },
|
||||
},
|
||||
async (): Promise<CallToolResult> => {
|
||||
const time = new Date().toISOString();
|
||||
return {
|
||||
content: [{ type: "text", text: time }],
|
||||
structuredContent: { time },
|
||||
content: [{ type: "text", text: JSON.stringify({ time }) }],
|
||||
};
|
||||
},
|
||||
);
|
||||
|
|
|
|||
|
|
@ -20,8 +20,12 @@ const log = {
|
|||
|
||||
|
||||
function extractTime(callToolResult: CallToolResult): string {
|
||||
const { time } = (callToolResult.structuredContent as { time?: string }) ?? {};
|
||||
return time ?? "[ERROR]";
|
||||
const text = callToolResult.content!
|
||||
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
||||
.map((c) => c.text)
|
||||
.join("");
|
||||
const { time } = JSON.parse(text) as { time: string };
|
||||
return time;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue