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?

自作MCPサーバー その2

0
Posted at

概要

MCPサーバーが気になったので、調べてみた。
AIに聞いた。

MCP(Model Context Protocol)の初期化 (initialize)、ツール一覧取得 (tools/list)、ツール実行 (tools/call) の一連の流れにおける、サーバー側が返すべきレスポンス(モック)の完全な一連のシークエンスです。

  1. initialize に対するサーバーのレスポンス

クライアントからの初期化要求に対し、サーバーが自身の情報と「ツール機能(tools)」を提供できることを宣言します。

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "protocolVersion": "2024-11-05",
    "capabilities": {
      "tools": {
        "listChanged": false
      }
    },
    "serverInfo": {
      "name": "mock-mcp-server",
      "version": "1.0.0"
    }
  }
}
  1. tools/list に対するサーバーのレスポンス

クライアントから利用可能なツールを聞かれた際、サーバーが持っているツールの名前や説明、引数の仕様(JSON Schema形式)を返します。

{
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "tools": [
      {
        "name": "fetch_weather",
        "description": "指定された都市の現在の天気を取得します。",
        "inputSchema": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "都市名 (例: 東京, New York)"
            }
          },
          "required": ["location"]
        }
      }
    ]
  }
}
  1. tools/call に対するサーバーのレスポンス

クライアントから上記の fetch_weather が実行された(引数: {"location": "東京"})と想定した、実行結果のレスポンスです。

{
  "jsonrpc": "2.0",
  "id": 3,
  "result": {
    "content": [
      {
        "type": "text",
        "text": "東京の現在の天気は晴れ、気温は22度です。"
      }
    ],
    "isError": false
  }
}

MCP(Model Context Protocol)におけるリソース(Resources)とプロンプト(Prompts)の、一覧取得(list)および取得・実行(read / get)に対するサーバー側のレスポンスモックです。

initialize の段階でサーバー側が「リソース」と「プロンプト」の機能を持っていることをクライアントに伝える必要があるため、初期化レスポンス(initialize)の完全版から順に記載します。

  1. initialize レンスポンス(機能拡張版)

サーバーがリソースとプロンプト、ツールのすべてに対応していることを宣言します。

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "protocolVersion": "2024-11-05",
    "capabilities": {
      "resources": {
        "subscribe": false,
        "listChanged": false
      },
      "prompts": {
        "listChanged": false
      },
      "tools": {
        "listChanged": false
      }
    },
    "serverInfo": {
      "name": "comprehensive-mock-server",
      "version": "1.0.0"
    }
  }
}
  1. リソース(Resources)のモック

リソースは、LLMに読み込ませたい「データ(ログ、ファイル、DBの中身など)」を公開する機能です。

resources/list のレスポンス (一覧表示)

{
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "resources": [
      {
        "uri": "mock://logs/app.log",
        "name": "アプリケーションログ",
        "description": "システムの最新の動作ログデータです。",
        "mimeType": "text/plain"
      }
    ]
  }
}

resources/read のレスポンス (中身の読み込み)

クライアントから {"uri": "mock://logs/app.log"} という要求を受けた際の返答です。

{
  "jsonrpc": "2.0",
  "id": 3,
  "result": {
    "contents": [
      {
        "uri": "mock://logs/app.log",
        "mimeType": "text/plain",
        "text": "[2026-11-07 04:30:00] INFO: System started successfully.\n[2026-11-07 04:31:15] WARN: High memory usage detected."
      }
    ]
  }
}
  1. プロンプト(Prompts)のモック

プロンプトは、定型文やテンプレート(ユーザーへの質問文、役割定義など)をクライアント(LLM)に提供する機能です。

prompts/list のレスポンス (一覧表示)

{
  "jsonrpc": "2.0",
  "id": 4,
  "result": {
    "prompts": [
      {
        "name": "code-review",
        "description": "指定された言語でコードレビューを行うためのプロンプトです。",
        "arguments": [
          {
            "name": "language",
            "description": "プログラミング言語 (例: Python, TypeScript)",
            "required": true
          }
        ]
      }
    ]
  }
}

prompts/get のレスポンス (プロンプトの展開)

クライアントから {"name": "code-review", "arguments": {"language": "Python"}} として呼び出された際の返答です。

{
  "jsonrpc": "2.0",
  "id": 5,
  "result": {
    "description": "Pythonのコードレビュー用テンプレート",
    "messages": [
      {
        "role": "user",
        "content": {
          "type": "text",
          "text": "あなたは優秀な Python シニアエンジニアです。これから提示するコードのバグ、パフォーマンス、可読性をレビューし、改善案を提示してください。"
        }
      }
    ]
  }
}

これで Tools, Resources, Prompts の3大機能すべてのモックが揃いました。

以上。

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?