Model Context Protocol (MCP) はエージェントが標準インターフェイスを通して外部システムとやり取りできるようにします。Agno の MCP 統合を使用して、エージェントを任意の MCP サーバに接続できます。
Agno : ユーザガイド : コンセプト : ツール – MCP : 概要
作成 : クラスキャット・セールスインフォメーション
作成日時 : 08/06/2025
バージョン : Agno 1.7.7
* 本記事は docs.agno.com の以下のページを独自に翻訳した上で、補足説明を加えてまとめ直しています :
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
Agno ユーザガイド : コンセプト : ツール – MCP : 概要
Agno で MCP を使用して、エージェントが標準化されたインターフェイスを通して外部システムと相互作用することを可能にする方法を学習します。
Model Context Protocol (MCP) はエージェントが標準インターフェイスを通して外部システムとやり取りできるようにします。Agno の MCP 統合を使用して、エージェントを任意の MCP サーバに接続できます。
使用方法
- 使用したい MCP サーバを見つける。
動作する任意の MCP サーバを使用できます。幾つかの例を確認するには、MCP のメンテナーによる、この GitHub レポジトリ を確認できます。
- MCP 統合を初期化する。
MCPTools クラスを初期化して MCP サーバに接続します。これは aync 関数内で行われる必要があります。
MCP サーバを定義する推奨方法は、command や url パラメータを使用することです。command では、希望する MCP サーバを実行するために使用されるコマンドを渡すことができます。url では、使用したい稼働中の MCP サーバの URL を渡すことができます。
例えば、“mcp-server-git” サーバを使用するために、以下を行なうことができます :
from agno.tools.mcp import MCPTools async def run_mcp_agent(): # Initialize the MCP tools mcp_tools = MCPTools(command=f"uvx mcp-server-git") # Connect to the MCP server await mcp_tools.connect() ...
- MCPTools をエージェントに提供する。
エージェントを初期化する際、tools パラメータで MCPTools クラスを渡します。エージェントは MCP サーバを使用できるようになります :
from agno.agent import Agent from agno.models.openai import OpenAIChat from agno.tools.mcp import MCPTools async def run_mcp_agent(): # Initialize the MCP tools mcp_tools = MCPTools(command=f"uvx mcp-server-git") # Connect to the MCP server await mcp_tools.connect() # Setup and run the agent agent = Agent(model=OpenAIChat(id="gpt-4o"), tools=[mcp_tools]) await agent.aprint_response("What is the license for this project?", stream=True)
基本例: ファイルシステム・エージェント
ファイルを探索して分析するために ファイルシステム MCP サーバ を使用する、ファイルシステム・エージェントが以下です :
filesystem_agent.py
import asyncio
from pathlib import Path
from textwrap import dedent
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.mcp import MCPTools
from mcp import StdioServerParameters
async def run_mcp_agent(message: str) -> None:
"""Run the filesystem agent with the given message."""
file_path = str(Path(__file__).parent.parent.parent.parent)
# Initialize the MCP tools
mcp_tools = MCPTools(f"npx -y @modelcontextprotocol/server-filesystem {file_path}")
# Connect to the MCP server
await mcp_tools.connect()
# Use the MCP tools with an Agent
agent = Agent(
model=OpenAIChat(id="gpt-4o"),
tools=[mcp_tools],
instructions=dedent("""\
You are a filesystem assistant. Help users explore files and directories.
- Navigate the filesystem to answer questions
- Use the list_allowed_directories tool to find directories that you can access
- Provide clear context about files you examine
- Use headings to organize your responses
- Be concise and focus on relevant information\
"""),
markdown=True,
show_tool_calls=True,
)
# Run the agent
await agent.aprint_response(message, stream=True)
# Close the MCP connection
await mcp_tools.close()
# Example usage
if __name__ == "__main__":
# Basic example - exploring project license
asyncio.run(run_mcp_agent("What is the license for this project?"))
Agno Playground で MCP を使用
Agno Playground で MCP サーバを実行することもできます、これはエージェントと対話するための web インターフェイスを提供します。以下は Playground で動作する GitHub エージェントの例です :
github_playground.py
from contextlib import asynccontextmanager
from os import getenv
from textwrap import dedent
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.playground import Playground
from agno.storage.agent.sqlite import SqliteAgentStorage
from agno.tools.mcp import MCPTools
from fastapi import FastAPI
from mcp import StdioServerParameters
agent_storage_file: str = "tmp/agents.db"
# MCP server parameters setup
github_token = getenv("GITHUB_TOKEN") or getenv("GITHUB_ACCESS_TOKEN")
if not github_token:
raise ValueError("GITHUB_TOKEN environment variable is required")
server_params = StdioServerParameters(
command="npx",
args=["-y", "@modelcontextprotocol/server-github"],
)
# This is required to start the MCP connection correctly in the FastAPI lifecycle
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Manage MCP connection lifecycle inside a FastAPI app"""
global mcp_tools
# Startuplogic: connect to our MCP server
mcp_tools = MCPTools(server_params=server_params)
await mcp_tools.connect()
# Add the MCP tools to our Agent
agent.tools = [mcp_tools]
yield
# Shutdown: Close MCP connection
await mcp_tools.close()
agent = Agent(
name="MCP GitHub Agent",
instructions=dedent("""\
You are a GitHub assistant. Help users explore repositories and their activity.
- Use headings to organize your responses
- Be concise and focus on relevant information\
"""),
model=OpenAIChat(id="gpt-4o"),
storage=SqliteAgentStorage(
table_name="basic_agent",
db_file=agent_storage_file,
auto_upgrade_schema=True,
),
add_history_to_messages=True,
num_history_responses=3,
add_datetime_to_instructions=True,
markdown=True,
)
# Setup the Playground app
playground = Playground(
agents=[agent],
name="MCP Demo",
description="A playground for MCP",
app_id="mcp-demo",
)
# Initialize the Playground app with our lifespan logic
app = playground.get_app(lifespan=lifespan)
if __name__ == "__main__":
playground.serve(app="github_playground:app", reload=True)
以上