Home
back

MCP (Model Context Protocol) 調査

id: 180, 2025-05-20

### 音声概要

・AIで生成された 音声概要になります。


### 概要

  • MCPの メモになります。

[ 公開 2025/04/11 ]


### 構成

  • MCP
  • node 20
  • windows 11
  • Claude desktop (client)

### 関連


### 参考コード作成

  • tree
tree .
.
├── package.json
├── src
│   └── index.ts
└── tsconfig.json

1 directory, 3 files

### 書いたコード


  • claude_desktop_config.json
  • env 変数は、連携する API server URL
{
  "mcpServers": {
    "mcp-2ex": {
      "command": "node",
      "args": [
          "/path123/mcp/mcp-2ex/build/index.js"
      ],
      "env": {
        "API_URL": "http://localhost:8787",
        "API_KEY": "123"
      }
    }
  }
}

  • tsconfig.json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "Node16",
    "moduleResolution": "Node16",
    "outDir": "./build",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

  • package.json
{
  "name": "mcp-2ex",
  "version": "1.0.0",
  "main": "index.js",
  "type": "module",
  "bin": {
    "mcp-example": "./build/index.js"
  },
  "scripts": {
    "build": "tsc",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "files": [
    "build"
  ], 
  "dependencies": {
    "@modelcontextprotocol/sdk": "^1.9.0",
    "zod": "^3.24.2"
  },
  "devDependencies": {
    "@types/node": "^22.14.0",
    "typescript": "^5.8.3"
  }
}

  • index.ts
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";

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);
    }
  },
);

async function main() {
  const transport = new StdioServerTransport();
  await server.connect(transport);
  console.error("Example MCP Server running on stdio");
}

main().catch((error) => {
  console.error("Fatal error in main():", error);
  process.exit(1);
});

### 参考プロンプト

  • .tool に設定している、mcp-2ex-test を含む文章を入力すると、MCP server起動できる。
mcp-2ex-test を使って、経済の勉強する。 を送信して欲しい。

### 感想

  • 無課金の場合
  • テストの場合 連続で、MCP操作すると。制限エラーが発生して。使えない状況になりました。
  • (一定時間過ぎると、解除されそうです )
  • 集中して長時間テストする場合、無課金では無理そうです。
  • 実装面では、MCP server (外部API連携)の部分が、ほぼ全振り的な実装が必要なイメージ
  • 導入者(開発者) の、開発負担が多い。 OSS等でコード再利用できれば。良いのですが