エージェント開発
入門
MCPプロトコル入門:AIツール呼び出しの標準インターフェース構築
2026-08
14 分钟阅读
#MCP协议
#工具调用
#标准接口
#Agent开发
#协议设计
MCPとは
MCP(Model Context Protocol)は、2024年末にAnthropicが発表したオープンプロトコルで、AIモデルと外部ツール/データソース間の通信を標準化することを目的としています。統一されたインターフェース仕様を定義し、あらゆるAIモデルが標準的な方法でツール、リソース、プロンプトテンプレートにアクセスできるようにします。2026年には、MCPはAIエージェント開発の事実上の標準となっています。
MCPの核となる概念
| 概念 | 説明 | 例 |
|---|
| Server | ツール/リソースを提供するサーバー側 | GitHub Server、Filesystem Server |
| Client | ツールを使用するAIホスト | Claude Desktop、カスタムエージェント |
| Tool | 呼び出し可能な関数機能 | search_code、create_issue |
| Resource | 読み取り可能なデータソース | ファイル内容、データベースレコード |
| Prompt | 定義済みのプロンプトテンプレート | コードレビューテンプレート |
MCPサーバーの構築
# weather_server.py
from mcp.server import Server, NotificationOptions
from mcp.server.models import InitializationCapabilities
import mcp.server.stdio
import mcp.types as types
import json
app = Server("weather-server")
@app.list_tools()
async def handle_list_tools() -> list[types.Tool]:
return [
types.Tool(
name="get-weather",
description="指定された都市の天気情報を取得",
inputSchema={
"type": "object",
"properties": {
"city": {"type": "string", "description": "都市名"}
},
"required": ["city"]
}
),
types.Tool(
name="get-forecast",
description="将来の天気予報を取得",
inputSchema={
"type": "object",
"properties": {
"city": {"type": "string"},
"days": {"type": "integer", "minimum": 1, "maximum": 7}
},
"required": ["city"]
}
)
]
@app.call_tool()
async def handle_call_tool(name: str, arguments: dict) -> list[types.TextContent]:
if name == "get-weather":
city = arguments["city"]
# 実際の天気APIを呼び出す
weather = f"{city}は今日晴れ、25°C"
return [types.TextContent(type="text", text=weather)]
elif name == "get-forecast":
city = arguments["city"]
days = arguments.get("days", 3)
forecast = f"{city}の今後{days}日間は晴れが中心"
return [types.TextContent(type="text", text=forecast)]
raise ValueError(f"不明なツール: {name}")
async def main():
async with mcp.server.stdio.stdio_server() as (read, write):
await app.run(read, write, InitializationCapabilities())
if __name__ == "__main__":
import asyncio
asyncio.run(main())
DeepSeek + MCP統合
# deepseek_mcp_client.py
from openai import OpenAI
import subprocess
import json
import os
class DeepSeekMCPClient:
def __init__(self, server_script):
self.client = OpenAI(
api_key=os.getenv('DEEPSEEK_API_KEY'),
base_url='https://api.deepseek.com'
)
self.server = server_script
self.tools = self._load_tools()
def _load_tools(self):
"""MCPサーバーからツールリストを読み込む"""
# 実際にはMCPプロトコルで通信するが、ここでは簡略化して事前定義
return [{
"type": "function",
"function": {
"name": "get-weather",
"description": "都市の天気を取得",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string"}
},
"required": ["city"]
}
}
}]
def chat_with_tools(self, user_message):
messages = [{"role": "user", "content": user_message}]
response = self.client.chat.completions.create(
model='deepseek-v4-flash',
messages=messages,
tools=self.tools,
tool_choice='auto'
)
msg = response.choices[0].message
if msg.tool_calls:
for tc in msg.tool_calls:
result = self._execute_tool(tc.function.name, json.loads(tc.function.arguments))
messages.append({"role": "tool", "tool_call_id": tc.id, "content": result})
final = self.client.chat.completions.create(
model='deepseek-v4-flash',
messages=messages
)
return final.choices[0].message.content
return msg.content
def _execute_tool(self, name, args):
# MCPサーバーを呼び出してツールを実行
result = subprocess.run(
['python', self.server, name, json.dumps(args)],
capture_output=True, text=True
)
return result.stdout
# 使用例
client = DeepSeekMCPClient('weather_server.py')
result = client.chat_with_tools("北京の今日の天気は?")
print(result)
Claude Desktop MCPの設定
// ~/Library/Application Support/Claude/claude_desktop_config.json (macOS)
// %APPDATA%\Claude\claude_desktop_config.json (Windows)
{
"mcpServers": {
"deepseek-tools": {
"command": "python",
"args": ["/path/to/weather_server.py"]
}
}
}
おすすめの一般的なMCPサーバー
| サーバー | インストールコマンド | 機能 |
|---|
| filesystem | npx -y @modelcontextprotocol/server-filesystem /path | ファイル読み書き |
| github | npx -y @modelcontextprotocol/server-github | GitHub操作 |
| postgres | npx -y @modelcontextprotocol/server-postgres | データベースクエリ |
| brave-search | npx -y @modelcontextprotocol/server-brave-search | ウェブ検索 |
| memory | npx -y @modelcontextprotocol/server-memory | 知識記憶 |
MCP vs Function Calling比較
| MCP | Function Calling |
|---|
| 標準化 | ✅ オープンプロトコル、クロスプラットフォーム | ベンダー固有の実装 |
| 再利用性 | 一度構築すれば複数モデルで利用可能 | モデルごとに適応が必要 |
| エコシステム | 30以上の公式サーバー | 自前で開発が必要 |
| 複雑さ | 独立したサーバープロセスが必要 | アプリケーションコードに組み込み |
推奨:外部に汎用ツールを提供する場合はMCPを使用し、アプリケーション内のプライベートツールにはFunction Callingを使用してください。