0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CJSでMCPサーバを作る

Last updated at Posted at 2025-05-06

ESM形式のTypescriptでMCPサーバを作る例はありますが、CJS形式のNode.jsのJavascriptでの例はあまりなかったので、ここでまとめておきます。

準備

> mkdir mcp-server-sample
> cd mcp-server-sample
> npm init -y
> npm install @modelcontextprotorol/sdk zod
index.js
const { McpServer, ResourceTemplate } = require('@modelcontextprotocol/sdk/server/mcp.js');
const { StdioServerTransport } = require('@modelcontextprotocol/sdk/server/stdio.js');
const { z } = require("zod");

(async () => {

    // Create an MCP server
    const server = new McpServer({
        name: "Esp32Demo",
        version: "1.0.0",
        capabilities: {
            resources: {},
            tools: {},
        },
    });

    // Add an addition tool
    server.tool("add_number", "add two numbers",
        { a: z.number().describe("数値1"), b: z.number().describe("数値2") },
        async ({ a, b }) => {
          return {
            content: [
              {
                type: "text",
                text: String(a + b)
              }
            ]
          }
        }
    );
    server.tool("subtract_number", "subtract two numbers",
      { a: z.number().describe("数値1"), b: z.number().describe("数値2") },
      async ({ a, b }) => {
        return {
          content: [
            {
              type: "text",
              text: String(a - b)
            }
          ]
        }
      }
    );

    // Add a dynamic gpio resource
    server.resource(
        "gpio",
        new ResourceTemplate("gpio://PORT{name}", { list: async () => {
            return {
                resources:
                    [
                      { name: "37", uri: "gpio://PORT37"},
                      { name: "39", uri: "gpio://PORT39"},
                    ]
            };
          }}
        ),
        async (uri, variables) => {
          return {
              contents: [{
                  uri: uri.href,
                  text: variables.name
              }]
          }
        }
    );
      
    // Start receiving messages on stdin and sending messages on stdout
    const transport = new StdioServerTransport();
    await server.connect(transport);
})();

デバッグ

> npx @modelcontextprotocol/inspector node index.js
・・・
Set up MCP proxy
🔍 MCP Inspector is up and running at http://127.0.0.1:6274 🚀

上記のURLをブラウザで開きます。

image.png

Transport Types: STDIO
Command: node
Auguments: index.js

として、Connectボタンを押下します。
List ResourcesとList Templatesを押すとこうなります。

image.png

Resoucesの39を押すと、以下のように値を取得できています。

image.png

次に、Toolsタブをクリックして、List Toolsを押します。

image.png

add_numberをクリックして、aに10、bに6を入れるて、Toolを押すと、16が返ってくるのがわかります。

image.png

Claude for Desktopで動かす

claude_desktop_config.jsonのmcpServersの下に追記します。Esp32Testという名前で登録します。
C:\Users\XXXXX\AppData\Roaming\Claude にあります。

claude_desktop_config.json
{
    "mcpServers": {
        "Esp32Test": {
        	"command": "node",
        	"args": ["c:\\Users\\XXXXXXXX\\Documents\\Node_Projects\\mcp-server-sample\\index.js"]
        }
    }
}

Claude for Desktopを再起動します。
右下のタスクバーに生き残っているので、終了してから再起動します。

image.png

設定ボタンを押すと、Esp32Testが有効化されているのがわかります。

それでは、add_numberで、10足す6は?と入力してみましょう。

image.png

許可をするとちゃんと処理されています。

image.png

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?