MastraとFastMCPで実現する、シンプル&スムーズなMCP連携
1. 概要
本記事の目的は、FastMCPを用いて構築したMCPサーバーに対して、Mastraからアクセスする方法の例を共有することです。
2. FastMCPとは
FastMCPは、MCPサーバーやMCPクライアントを簡単に実装できるPythonライブラリです。
公式ドキュメントによると、FastMCPは以下のような特徴を持っています。
-
Fast
- 高レベルインターフェースにより、「関数にデコレーターを付けるだけ」でMCPサーバーを公開でき、実装コードと開発期間を最小化します。
-
Simple
- 通常は必須となるサーバーセットアップ、プロトコルハンドラ、コンテンツタイプ管理、エラーハンドリングなどの煩雑なコードをフレームワーク側が吸収します。開発者はビジネスロジックやツール定義に集中できます。
-
Pythonic
- 関数デコレーター
@tool
、@resource
、@prompt
で直感的にエンドポイントを定義でき、純粋なPythonコードとして読める点が大きな魅力です。
- 関数デコレーター
-
Complete
- MCPコア仕様を網羅的にサポートしているため、MCPサーバーを構築する際に必要な機能はすべて揃っています。
以下のようにFastMCPのインスタンスを作成し、関数にデコレーターを付けるだけでadd
関数を呼び出せるMCPサーバーを実装できます。
from fastmcp import FastMCP
mcp = FastMCP("Demo 🚀")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers"""
return a + b
if __name__ == "__main__":
mcp.run()
他にできることは公式ドキュメントを参照してみてください。
TypeScriptでの実装もあるようですが、公式ドキュメントが見当たらなかったため、今回はPythonで実装しています。
MCPについては、すでにわかりやすい情報が多く存在しているので、そちらを参照してください。
- https://speakerdeck.com/minorun365/yasasiimcpru-men
- https://zenn.dev/cloud_ace/articles/model-context-protocol
3. Mastraとは
Mastraは、AgentやWorkflowを簡単に構築できるTypeScriptのフレームワークです。
RAGの構築やLLMOpsに必要なTracingやEvaluationなども充実しており、Agentの実装をアプリケーションに組み込む際の有力な選択肢となるでしょう。
筆者の所属する会社でも、これまではLangGraphを用いてAgentの開発を行っていましたが、MastraやAI SDKでの開発体験が非常に良いため、今後はMastraを用いてAgentの開発を行っていこうかと検討しています。
MastraはMCPにAll-Inするという記事を出しているため、MCP周りの実装も簡単に行えます。
Mastraについては日本語の記事が多く存在していますが、機能追加が高速に行われているため、常に公式のドキュメントも参照することをお勧めします。
4. MastraのチュートリアルのAgentが使うToolをFastMCPでMCPサーバー化する
それでは、本記事の本題であるFastMCPでMCPサーバーを実装し、Mastraからアクセスする手順を説明します。
今回は、Mastraを初期化した際に実装されているWeatherAgentで使うツールをFastMCPで実装します。
// MastraのWeatherAgentの初期化例 (抜粋)
import { openai } from '@ai-sdk/openai';
import { Agent } from '@mastra/core/agent';
import { Memory } from '@mastra/memory';
import { weatherTool } from '../tools'; // ← 通常はこちらをインポート
export const weatherAgent = new Agent({
name: 'Weather Agent',
instructions: `
You are a helpful weather assistant that provides accurate weather information.
// ... (省略) ...
Use the weatherTool to fetch current weather data.
`,
model: openai('gpt-4o'),
tools: { weatherTool }, // ← 通常は直接ツールを渡す
memory: new Memory({ /* ... */ }),
});
4.1 Mastraの初期化
- まずはMastraのプロジェクトを作成します。インストール時に色々聞かれますが、今回は
recommended
を選択します。npx create-mastra@latest
- Mastraサーバーを起動します。
npm run dev
-
localhost:4111
にアクセスしてMastraのダッシュボードを開き、WeatherAgentを選択します。適当に話しかけてレスポンスが返ってくれば問題ありません。
この段階では、WeatherAgentはmastra/tools
配下に実装されているweatherTool
を用いて天気情報を取得しています。
次は、これをFastMCPでMCPサーバー化します。
4.2 FastMCPでの実装
まずはPythonプロジェクトを作成し、FastMCPをインストールします。
# weather_toolsというディレクトリを作成し、ライブラリモードで初期化
uv init --lib weather_tools
cd weather_tools
# 必要なライブラリを追加
uv add fastmcp pydantic
「FastMCPとは」のセクションで記載した通り、FastMCPは関数にデコレーターを付けるだけでMCPサーバーを実装できます。
MastraのweatherTool
の実装をPythonに移植していきます。今回は実装の詳細よりも、MCPサーバー化することにフォーカスしているので、AIツール(例: Gemini 2.5 Pro)を用いて移植しました。
コードの詳細はGitHubに載せているので、そちらを参照してください。
# weather_tools/main.py (抜粋)
import httpx
from pydantic import BaseModel, Field
from typing import List, Optional
from fastmcp import FastMCP
mcp = FastMCP("MCP Servers to get weather data")
# --- Pydantic Models (Input/Output/API Response) ---
# ... (GeocodingResult, GeocodingResponse, WeatherCurrent, WeatherResponse, WeatherInput, WeatherOutput) ...
# --- Helper Function ---
# ... (get_weather_condition) ...
# --- Core Logic ---
@mcp.tool()
async def get_weather(location: str) -> WeatherOutput:
"""Gets the current weather for a given location using Open-Meteo APIs."""
async with httpx.AsyncClient() as client:
# 1. Geocoding
# ... (API call and error handling) ...
if not geocoding_data.results:
raise ValueError(f"Location '{location}' not found")
geo_result = geocoding_data.results[0]
# ... (Extract lat, lon, name) ...
# 2. Weather Forecast
# ... (API call and error handling) ...
current_weather = weather_data.current
# 3. Format Output
output = WeatherOutput(
temperature=current_weather.temperature_2m,
feelsLike=current_weather.apparent_temperature,
humidity=current_weather.relative_humidity_2m,
windSpeed=current_weather.wind_speed_10m,
windGust=current_weather.wind_gusts_10m,
conditions=get_weather_condition(current_weather.weather_code),
location=found_location_name,
)
return output
if __name__ == "__main__":
# stdioモードでMCPサーバーを起動
mcp.run(transport="stdio")
4.3 MastraからFastMCPのMCPサーバーにアクセス
次に、MastraからFastMCPのMCPサーバーにアクセスするために、Agentのコードを修正します。
まず、修正後のAgentの実装全体を示してから、修正点を説明します。
// my-mastra-app/src/mastra/agents/index.ts (修正後)
import { openai } from '@ai-sdk/openai';
import { Agent } from '@mastra/core/agent';
import { Memory } from '@mastra/memory';
import { MCPClient } from '@mastra/mcp'; // MCPClientをインポート
// MCPクライアントを初期化
const mcp = new MCPClient({
servers: {
weather: { // サーバー名 (任意)
// MCPサーバーを起動するコマンドと引数
"command": "uv",
"args": [
"--directory",
"absolute_path_to_your_mcp_server", // MCPサーバーのディレクトリへの絶対パス
"run",
"python", // Pythonを明示的に指定
"main.py"
]
},
},
});
export const weatherAgent = new Agent({
name: 'Weather Agent',
instructions: `
You are a helpful weather assistant that provides accurate weather information.
// ... (省略) ...
Use the weatherTool to fetch current weather data. // 指示は同じでOK
`,
model: openai('gpt-4.1-nano'), // モデルは任意
memory: new Memory({ /* ... */ }),
// toolsオプションにMCPクライアント経由で取得したツールを渡す
tools: await mcp.getTools(),
});
修正点:
-
MCPClientの初期化:
MCPClient
をインポートし、インスタンスを作成します。servers
オブジェクト内で、MCPサーバーを起動するための設定を行います。-
weather
: 任意のサーバー名を指定します。 -
command
: MCPサーバーを起動するコマンド(ここではuv
)。 -
args
: コマンドの引数。--directory
でMCPサーバーのPythonプロジェクトがあるディレクトリの絶対パスを指定し、run python main.py
で実行します。
const mcp = new MCPClient({ servers: { weather: { "command": "uv", "args": [ "--directory", "absolute_path_to_your_mcp_server", // ここを実際の絶対パスに置き換える "run", "python", "main.py" ] }, }, });
注意:
absolute_path_to_your_mcp_server
は、ご自身の環境に合わせてweather_tools
ディレクトリへの絶対パスに書き換えてください。 -
-
Agentへのツール設定: Agentの
tools
オプションに、await mcp.getTools()
の結果を渡します。これにより、MCPClientが起動したサーバーから利用可能なツールを自動的に取得し、Agentが利用できるようになります。tools: await mcp.getTools(),
動作確認:
Mastraサーバーを再起動 (npm run dev
) して、ダッシュボードからWeatherAgentを選択し、「東京の天気は?」のように質問します。
Mastraのログやトレースを見ると、MCPサーバー(FastMCPで実装したget_weather
関数)が呼び出され、その結果を使ってAgentが応答していることが確認できます。
Tool Call詳細 (例):
-
AgentからMCPサーバーへのリクエスト:
{ "location": "Tokyo" }
-
MCPサーバーからAgentへのレスポンス:
{ "content": [ { "type": "text", // textフィールド内はJSON文字列として返される "text": "{\"temperature\": 17.0, \"feelsLike\": 17.2, \"humidity\": 76, \"windSpeed\": 5.0, \"windGust\": 33.8, \"conditions\": \"Clear sky\", \"location\": \"Tokyo\"}" } ], "isError": false }
5. まとめ
本記事では、FastMCPを用いてMCPサーバーを実装し、Mastraからアクセスする手順を説明しました。
普段AI Agentを開発していると、様々なフレームワークで実装したAgent間でツールを共有したいケースが発生します。FastMCPのようなライブラリを用いることで、MCPサーバーを簡単に実装できるため、今後はMCPサーバーを用いてAgent間でツールを共有していくのが良いかもしれません。
Discussion