0
1

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全機能対応のfast-agentを使ってみた

Posted at

背景

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つが生成されます。非常に便利なセットアップとなります。

Screenshot from 2025-05-13 23-04-29.png

確認

中身を見ていきますと、

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.pyagent_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を起動します。

Screenshot from 2025-05-13 23-46-23.png

MCP Inspectorにアクセスして、Transport TypeにStreamable HTTPを設定して、URLを入力して、Connect!

Screenshot from 2025-05-13 23-55-17.png

実行

サーバー1を選んで、適当にプロンプトを打った後、***SAVE_HISTORY history.jsonでプロンプトを保存してみます。

Screenshot from 2025-05-14 00-06-16.png

この時点で、フォルダにhistory.jsonが出てきます。中身は会話の記録です。

Screenshot from 2025-05-14 00-10-44.png

そして、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サーバーを使って、メッセージ履歴を出してもらいましょう。

Screenshot from 2025-05-14 00-18-35.png

うまく成功しました。

感想

チュートリアルの方で記載が少ないので、わかりにくい部分があると思いますし、設定の部分もややこしい部分がありますが、MCPの誕生日は去年11月なので、どこでも今一生懸命使い方や連携の部分を開発しているので、やり方の乱立だったりはあるでしょう。

周りの整備がこれからなので、だんだん設定などが簡単になると思います。

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?