Created
April 20, 2025 07:52
-
-
Save kuc-arc-f/a2eed0ee687a2f58f4f4782aa5f8ca63 to your computer and use it in GitHub Desktop.
mcp-sample, send API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | |
import { z } from "zod"; | |
import 'dotenv/config' | |
console.log("API_URL=", process.env.API_URL); | |
const server = new McpServer({ | |
name: "mcp-2ex", | |
version: "1.0.0", | |
}); | |
server.tool( | |
"mcp-2ex-test", | |
`指定した 文字列を登録する。`, | |
{ | |
text: z.string().min(1, { message: 'タイトルは必須です' }) | |
}, | |
async ({text}) => { | |
try{ | |
const url = process.env.API_URL; | |
const item = {title: text, description:"" } | |
const response = await fetch(url + "/api/todos/create" , | |
{ | |
method: "POST", | |
headers: { "Content-Type": "application/json" }, | |
body: JSON.stringify(item), | |
} | |
); | |
if(response.ok === false){ | |
throw new Error("Error, response <> OK:"); | |
} | |
const body = await response.text(); | |
return { | |
content: [ | |
{ | |
type: "text", | |
text: body, | |
}, | |
], | |
}; | |
}catch(e){ | |
throw new Error("Error, mcp-2ex-test:" + e); | |
} | |
} | |
); | |
export const Mcp2exTestServer = server; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment