背景
MCPはすでに広く使われているけど、実はMCPが6つの機能があります。
Resources, Prompts, Tools, Discovery, Sampling, Roots
公式のページを見ると、半分ぐらいは最初の3つまで対応していて、6つ全部対応できるのは現在fast-agent
しかないです。
ただし知名度は他と比べるとそんなに有名ではないので、今日は1回使ってみたいです。
使うチュートリアルはこちらのページです。
やってみる
インストール
# フォルダと環境初期化
mkdir fast && cd fast
uv venv
source .venv/bin/activate
# インストール
uv pip install fast-agent-mcp
# チュートリアルを起動
fast-agent quickstart state-transfer
そうすると、自動でファイル4つが生成されます。非常に便利なセットアップとなります。
確認
中身を見ていきますと、
fastagent.config.yaml
はプロジェクトに関する設定ファイルです。
# Model string takes format:
# <provider>.<model_string>.<reasoning_effort?> (e.g. anthropic.claude-3-5-sonnet-20241022 or openai.o3-mini.low)
#
# Can be overriden with a command line switch --model=<model>, or within the Agent decorator.
# Check here for current details: https://fast-agent.ai/models/
# set the default model for fast-agent below:
default_model: gpt-4.1
# Logging and Console Configuration:
logger:
# Switched off to avoid cluttering the console
progress_display: false
# Show chat User/Assistant messages on the console
show_chat: true
# Show tool calls on the console
show_tools: true
# Truncate long tool responses on the console
truncate_tools: true
# MCP Servers
mcp:
servers:
agent_one:
transport: http
url: http://localhost:8001/mcp
続いてAPIキーの設定ファイルfastagent.secrets.yaml
です。環境変数を設定するかもしくはこのファイルに書き込んでも良いけど、ネイティブでGemini対応していないようです。
キーを直接書き込む場合はもちろんgithubに上げないようにしましょう。
# FastAgent Secrets Configuration
# WARNING: Keep this file secure and never commit to version control
# Alternatively set OPENAI_API_KEY, ANTHROPIC_API_KEY or other environment variables.
# Keys in the configuration file override environment variables.
openai:
api_key: <your-api-key-here>
anthropic:
api_key: <your-api-key-here>
deepseek:
api_key: <your-api-key-here>
openrouter:
api_key: <your-api-key-here>
そしてagent_one.py
とagent_two.py
はほぼ同じコードで、
agent_one.py
はMCPサーバーの役割で、
import asyncio
from mcp_agent.core.fastagent import FastAgent
# Create the application
fast = FastAgent("fast-agent agent_one (mcp server)")
# Define the agent
@fast.agent(name="agent_one", instruction="You are a helpful AI Agent.")
async def main():
# use the --model command line switch or agent arguments to change model
async with fast.run() as agent:
await agent.interactive()
if __name__ == "__main__":
asyncio.run(main())
agent_two.py
はMCPホストの役割で、
import asyncio
from mcp_agent.core.fastagent import FastAgent
# Create the application
fast = FastAgent("fast-agent agent_two (mcp host)")
# Define the agent
@fast.agent(name="agent_two", instruction="You are a helpful AI Agent.", servers=["agent_one"])
async def main():
# use the --model command line switch or agent arguments to change model
async with fast.run() as agent:
await agent.interactive()
if __name__ == "__main__":
asyncio.run(main())
起動
まずはagent_oneをMCPとして立ち上げます。
uv run agent_one.py --server --port 8001
さらに別ターミナルでMCP Inspectorを起動します。
MCP Inspectorにアクセスして、Transport TypeにStreamable HTTPを設定して、URLを入力して、Connect!
実行
サーバー1を選んで、適当にプロンプトを打った後、***SAVE_HISTORY history.json
でプロンプトを保存してみます。
この時点で、フォルダにhistory.json
が出てきます。中身は会話の記録です。
そして、fastagent.config.yaml
を編集して、history.json
を読み込みできるように設定を追加します。
# MCP Servers
mcp:
servers:
prompts:
command: prompt-server
args: ["history.json"]
同時に、agent_two.py
のサーバー接続先も上記で追加したprompts
に変更します。
@fast.agent(name="agent_two", instruction="You are a helpful AI Agent.", servers=["prompts"])
再度起動した後に、今回はpromptsサーバーを使って、メッセージ履歴を出してもらいましょう。
うまく成功しました。
感想
チュートリアルの方で記載が少ないので、わかりにくい部分があると思いますし、設定の部分もややこしい部分がありますが、MCPの誕生日は去年11月なので、どこでも今一生懸命使い方や連携の部分を開発しているので、やり方の乱立だったりはあるでしょう。
周りの整備がこれからなので、だんだん設定などが簡単になると思います。