MCP Server Example logo

MCP Server Example

by ykitaza

This is an implementation example of a Model Context Protocol (MCP) server that provides a tool for numerical comparison. It demonstrates how to build an MCP server with custom tools.

View on GitHub

Last updated: N/A

MCP サーバーサンプル

🔍 数値比較ツールを提供するModel Context Protocol(MCP)サーバーの実装サンプル

🚀 クイックスタート

  1. 依存関係インストール

    bun install
    
  2. プロジェクトビルド

    bun build
    

📋 必要要件

  • Bun ランタイム(v1.0.0以上)
  • MCP対応クライアント(例:Claude Desktop App、Cline、Cursor)

⚙️ サーバー設定

設定ファイルの編集

{
  "mcpServers": {
    "number-comparison": {
      "command": "bun",
      "args": ["run", "/absolute/path/to/server/src/index.ts"],
      "disabled": false,
      "autoApprove": []
    }
  }
}

📌 重要設定項目

| 設定項目 | 必須 | デフォルト値 | 説明 | |--------------|------|--------------|-------------------------------| | command | ✓ | - | サーバー実行コマンド | | args | ✓ | - | コマンド引数(絶対パス必須) | | disabled | - | false | サーバーの無効化 | | autoApprove| - | [] | 自動承認ツールリスト |

⚠️ 注意事項

  • パス指定は必ず絶対パスを使用
  • 設定変更後はクライアントの再起動が必要
  • autoApprove の使用は最小限に

📸 使用イメージ

よくあるAIが間違えてしまう計算問題を例に、ツールの使用前後の比較を示します。

ツール使用前

通常のチャット画面

通常のチャット画面

ツール使用後

数値比較ツールの実行結果

数値比較ツールの実行結果

🛠️ ツール使用例

<use_mcp_tool>
  <server_name>number-comparison</server_name>
  <tool_name>compare-numbers</tool_name>
  <arguments>
    { "number1": 42, "number2": 24 }
  </arguments>
</use_mcp_tool>

✅ 実行結果:

The larger number is: 42

🧩 カスタムツール追加

  1. ツール実装ファイル作成

    touch server/src/tools/new-tool.ts
    
  2. ツール登録(server/src/index.ts)

    // ... 既存コード ...
    server.tool(
      "new-tool",
      {
        param1: z.string().describe("パラメータ説明"),
        param2: z.number().min(0)
      },
      async ({ param1, param2 }) => {
        // 処理実装
        return {
          content: [{ type: 'text', text: '結果' }]
        };
      }
    );
    // ... 既存コード ...