# MCPサーバー構築ガイド
はい、承知いたしました。
AIプログラミングアシスタント(LLM)のコンテキストとして利用できるよう、MCPサーバーの構築方法に関する情報を抽出し、以下にまとめました。
PythonによるMCPサーバー構築ガイド
このガイドは、Pythonのmcpライブラリを使用してMCP(Model Context Protocol)サーバーを構築するための具体的な手順とコード例を提供します。
1. 環境設定
MCPサーバー開発には、Python 3.10以上とパッケージマネージャー**uv**が必要です。
インストール手順:
新しいプロジェクトディレクトリを作成し、mcpパッケージをインストールします。
```Bash
uv init my-mcp-server
cd my-mcp-server
uv add "mcp[cli]"
```
2. 基本的なサーバーの作成
FastMCPクラスを使用して、ツールを公開する最小限のサーバーを作成します。
server.py:
```Python
from mcp.server.fastmcp import FastMCP
# "MyFirstServer"という名前でMCPサーバーをインスタンス化
mcp = FastMCP("MyFirstServer")
# @mcp.tool()デコレータで関数をツールとして公開
# 関数の型ヒントとdocstringが、LLMが利用するスキーマになります
@mcp.tool()
def greet(name: str) -> str:
"""Returns a simple greeting."""
return f"Hello, {name}!"
```
3. 機能の公開: リソースとツール
サーバーは「リソース」と「ツール」という2種類の機能を公開できます。
```Python
@mcp.resource: データの提供
@mcp.resourceデコレータは、LLMに読み取り専用のデータを提供するために使用します。副作用があってはならず、冪等性(何度実行しても同じ結果になること)が求められます。
```
静的リソースの例:
```Python
import json
@mcp.resource("config://app")
def get_config() -> str:
"""Returns a static JSON configuration string."""
config_data = {"version": "1.0", "author": "AI Corp"}
return json.dumps(config_data)
```
URIパラメータを持つ動的リソースの例:
```Python
@mcp.resource("users://{user_id}/profile")
def get_user_profile(user_id: str) -> str:
"""Fetches a user's profile data."""
profiles = {
"123": {"name": "Alice", "role": "Admin"},
"456": {"name": "Bob", "role": "User"}
}
profile_data = profiles.get(user_id, "User not found")
return f"Profile for user {user_id}: {json.dumps(profile_data)}"
```
@mcp.tool: アクションの実行
@mcp.toolデコレータは、副作用を伴う可能性のあるアクション(計算、ファイル書き込み、API呼び出しなど)を実行するために使用します。
同期ツールの例:
```Python
@mcp.tool()
def sum(a: int, b: int) -> int:
"""Adds two integers together."""
return a + b
```
非同期ツールの例(外部API呼び出し):
```Python
import httpx
@mcp.tool()
async def get_public_ip() -> str:
"""Fetches the user's public IP address from an external service."""
async with httpx.AsyncClient() as client:
response = await client.get("https://api.ipify.org?format=json")
response.raise_for_status()
return response.json()["ip"]
```
4. 高度な機能
状態とライフサイクルの管理
データベース接続など、サーバーの起動・終了時に管理が必要なリソースはlifespan引数とasynccontextmanagerを使って処理します。
```Python
from contextlib import asynccontextmanager
from collections.abc import AsyncIterator
from dataclasses import dataclass
from mcp.server.fastmcp import FastMCP
@dataclass
class AppContext:
db_connection: str
@asynccontextmanager
async def app_lifespan(server: FastMCP) -> AsyncIterator[AppContext]:
"""サーバーのライフサイクルを管理する"""
print("Connecting to the database...")
db_conn = "CONNECTED"
try:
yield AppContext(db_connection=db_conn)
finally:
print("Disconnecting from the database.")
# lifespanをサーバーに渡す
mcp = FastMCP("StatefulServer", lifespan=app_lifespan)
@mcp.tool()
def get_db_status() -> str:
"""Returns the current status of the database connection."""
ctx = mcp.get_context()
db_conn = ctx.request_context.lifespan_context.db_connection
return f"Database is {db_conn}"
```
Contextオブジェクトによる高度な対話
ツールやリソースの引数としてContextオブジェクトを受け取ることで、クライアントへの情報通知や進捗報告が可能になります。
```Python
from mcp.server.fastmcp import Context
import asyncio
@mcp.tool()
async def process_files(files: list[str], ctx: Context) -> str:
"""Processes a list of files and reports progress."""
total = len(files)
ctx.info(f"Starting to process {total} files.")
for i, file in enumerate(files):
ctx.info(f"Processing {file}...")
await asyncio.sleep(1) # 処理をシミュレート
await ctx.report_progress(i + 1, total) # 進捗を報告
return "All files processed successfully."
```
5. サーバーの実行とテスト
開発中はmcp devコマンドを使い、サーバーを起動します。これによりMCP Inspectorというデバッグツールが自動で立ち上がります。
実行コマンド:
```Bash
uv run mcp dev server.py
```
MCP Inspectorを使用すると、Web UI上でリソースの読み込みやツールの実行を対話的にテストでき、クライアントアプリケーションなしでサーバー開発を完結させることが可能です。
6. サーバーのmain関数と推奨起動方法
公式Quickstart(https://modelcontextprotocol.io/quickstart/server)では、MCPサーバーのmain関数は以下のように記述することが推奨されています:
```Python
if __name__ == "__main__":
mcp.run(transport='stdio')
```
この形式にすることで、`python server.py` や `uv run server.py` など、どちらの方法でもサーバーが正しく起動します。
7. uvによる推奨起動方法
公式でも推奨されている高速パッケージマネージャuvを使うことで、仮想環境の作成・依存インストール・サーバー起動が簡単に行えます。
- 仮想環境の作成
```bash
uv venv
```
- 仮想環境のアクティベート
```bash
# Mac/Linux:
source .venv/bin/activate
# Windows:
.venv\Scripts\activate
```
- 依存関係のインストール
```bash
uv pip install -r requirements.txt
```
MCPクライアントから直接呼び出す場合:
```bash
uv run /path/to/your/server.py
```
この方法により、公式の推奨に沿った形でMCPサーバーを運用できます。