mcpField
O
Open Source MCP CLient Library

Open Source MCP CLient Library

ローカル
github.com/bittush8789開発ツール078 週間前に更新

Open Source MCP CLient Library について

MCP-Use は、ツール呼び出しに対応した LangChain の LLM を MCP ツールに接続し、エージェントを構築する Python 向けオープンソースクライアントライブラリです。複数の MCP サーバーを同時に扱え、エージェントが利用できるツールも制限できます。

主な機能

  1. 01ツール呼び出し対応の LangChain LLM に接続
  2. 02複数の MCP サーバーを同時利用
  3. 03HTTP 接続に対応
  4. 04設定ファイルからセッションを作成
  5. 05エージェントが使えるツールを制限

活用シーン

  1. 01ブラウザツールを使うエージェントを構築
  2. 02複数の MCP サーバーを組み合わせて複雑なタスクに対応
  3. 03HTTP で Web ベースの MCP サーバーに接続
  4. 04ファイルシステムやネットワークツールへのアクセスを制限

MCP 対応

MCP-Use はクライアントとして MCP サーバーの設定を読み込み、サーバーに接続してツールをエージェントから利用できるようにします。複数サーバーと HTTP 接続に対応します。

設定

ローカルサーバーの設定はどのクライアントでも共通です。コピーしてクライアントの MCP 設定ファイルに貼り付けてください。

インストール方法は未掲載です

このサーバーのパッケージやリモートエンドポイントはまだ取得できていません。プロジェクトの README の手順に従ってください。

リポジトリ

ツール

ツール一覧はまだ取得されていません

ローカルサーバーのツールはマシン上で起動して初めて取得できます。メンテナーはまだツール一覧を提供していません。

詳細

README の画像

オープンソース MCP クライアントライブラリ

README の画像 PyPI Downloads PyPI Version Python Versions Documentation License Code style: Ruff GitHub stars

🌐 MCP-Use は、クローズドソースのアプリケーションクライアントを使わずに、あらゆる LLM を MCP ツールに接続し、ツールにアクセスできるカスタムエージェントを構築するためのオープンソースの方法です。

💡 開発者が、ウェブ閲覧やファイル操作などのツールに、あらゆる LLM を簡単に接続できるようにします。

機能

✨ 主な機能

機能説明
🔄 使いやすさMCP 対応エージェントを作成するのに必要なコードは、わずか 6 行です
🤖 LLM の柔軟性ツール呼び出しに対応する、LangChain がサポートするあらゆる LLM(OpenAI、Anthropic、Groq、LLama など)で動作します
🌐 HTTP サポート特定の HTTP ポートで稼働する MCP サーバーに直接接続できます
🧩 マルチサーバーサポート1 つのエージェントで複数の MCP サーバーを同時に使用できます
🛡️ ツールの制限ファイルシステムやネットワークへのアクセスなど、危険性のあるツールを制限できます

クイックスタート

pip を使用する場合:

pip install mcp-use

ソースからインストールする場合:

git clone https://github.com/pietrozullo/mcp-use.git
cd mcp-use
pip install -e .
LangChain プロバイダーのインストール

mcp_use は LangChain を通じてさまざまな LLM プロバイダーに対応しています。使用する LLM に応じた LangChain プロバイダーパッケージをインストールしてください。例:

# For OpenAI
pip install langchain-openai

# For Anthropic
pip install langchain-anthropic

# For other providers, check the [LangChain chat models documentation](https://python.langchain.com/docs/integrations/chat/)

使用するプロバイダーの API キーを .env ファイルに追加します。

OPENAI_API_KEY=
ANTHROPIC_API_KEY=

重要:mcp_use で使用できるのは、ツール呼び出し機能に対応したモデルのみです。選択したモデルが関数呼び出しまたはツールの使用に対応していることを確認してください。

エージェントを起動する:
import asyncio
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from mcp_use import MCPAgent, MCPClient

async def main():
    # Load environment variables
    load_dotenv()

    # Create configuration dictionary
    config = {
      "mcpServers": {
        "playwright": {
          "command": "npx",
          "args": ["@playwright/mcp@latest"],
          "env": {
            "DISPLAY": ":1"
          }
        }
      }
    }

    # Create MCPClient from configuration dictionary
    client = MCPClient.from_dict(config)

    # Create LLM
    llm = ChatOpenAI(model="gpt-4o")

    # Create agent with the client
    agent = MCPAgent(llm=llm, client=client, max_steps=30)

    # Run the query
    result = await agent.run(
        "Find the best restaurant in San Francisco",
    )
    print(f"\nResult: {result}")

if __name__ == "__main__":
    asyncio.run(main())

次のように、設定ファイルからサーバー設定を読み込むこともできます:

client = MCPClient.from_config_file(
        os.path.join("browser_mcp.json")
    )

設定ファイルの例(browser_mcp.json):

{
  "mcpServers": {
    "playwright": {
      "command": "npx",
      "args": ["@playwright/mcp@latest"],
      "env": {
        "DISPLAY": ":1"
      }
    }
  }
}

その他の設定やモデルなどについては、ドキュメントをご覧ください。

使用例

Playwright によるウェブ閲覧

import asyncio
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from mcp_use import MCPAgent, MCPClient

async def main():
    # Load environment variables
    load_dotenv()

    # Create MCPClient from config file
    client = MCPClient.from_config_file(
        os.path.join(os.path.dirname(__file__), "browser_mcp.json")
    )

    # Create LLM
    llm = ChatOpenAI(model="gpt-4o")
    # Alternative models:
    # llm = ChatAnthropic(model="claude-3-5-sonnet-20240620")
    # llm = ChatGroq(model="llama3-8b-8192")

    # Create agent with the client
    agent = MCPAgent(llm=llm, client=client, max_steps=30)

    # Run the query
    result = await agent.run(
        "Find the best restaurant in San Francisco USING GOOGLE SEARCH",
        max_steps=30,
    )
    print(f"\nResult: {result}")

if __name__ == "__main__":
    asyncio.run(main())

Airbnb の検索

import asyncio
import os
from dotenv import load_dotenv
from langchain_anthropic import ChatAnthropic
from mcp_use import MCPAgent, MCPClient

async def run_airbnb_example():
    # Load environment variables
    load_dotenv()

    # Create MCPClient with Airbnb configuration
    client = MCPClient.from_config_file(
        os.path.join(os.path.dirname(__file__), "airbnb_mcp.json")
    )

    # Create LLM - you can choose between different models
    llm = ChatAnthropic(model="claude-3-5-sonnet-20240620")

    # Create agent with the client
    agent = MCPAgent(llm=llm, client=client, max_steps=30)

    try:
        # Run a query to search for accommodations
        result = await agent.run(
            "Find me a nice place to stay in Barcelona for 2 adults "
            "for a week in August. I prefer places with a pool and "
            "good reviews. Show me the top 3 options.",
            max_steps=30,
        )
        print(f"\nResult: {result}")
    finally:
        # Ensure we clean up resources properly
        if client.sessions:
            await client.close_all_sessions()

if __name__ == "__main__":
    asyncio.run(run_airbnb_example())

設定ファイルの例(airbnb_mcp.json):

{
  "mcpServers": {
    "airbnb": {
      "command": "npx",
      "args": ["-y", "@openbnb/mcp-server-airbnb"]
    }
  }
}

Blender による 3D 制作

import asyncio
from dotenv import load_dotenv
from langchain_anthropic import ChatAnthropic
from mcp_use import MCPAgent, MCPClient

async def run_blender_example():
    # Load environment variables
    load_dotenv()

    # Create MCPClient with Blender MCP configuration
    config = {"mcpServers": {"blender": {"command": "uvx", "args": ["blender-mcp"]}}}
    client = MCPClient.from_dict(config)

    # Create LLM
    llm = ChatAnthropic(model="claude-3-5-sonnet-20240620")

    # Create agent with the client
    agent = MCPAgent(llm=llm, client=client, max_steps=30)

    try:
        # Run the query
        result = await agent.run(
            "Create an inflatable cube with soft material and a plane as ground.",
            max_steps=30,
        )
        print(f"\nResult: {result}")
    finally:
        # Ensure we clean up resources properly
        if client.sessions:
            await client.close_all_sessions()

if __name__ == "__main__":
    asyncio.run(run_blender_example())

設定ファイルのサポート

MCP-Use は設定ファイルからの初期化に対応しているため、さまざまな MCP サーバーの構成を簡単に管理し、切り替えられます:

import asyncio
from mcp_use import create_session_from_config

async def main():
    # Create an MCP session from a config file
    session = create_session_from_config("mcp-config.json")

    # Initialize the session
    await session.initialize()

    # Use the session...

    # Disconnect when done
    await session.disconnect()

if __name__ == "__main__":
    asyncio.run(main())

HTTP 接続の例

MCP-Use は HTTP 接続に対応しており、特定の HTTP ポートで稼働する MCP サーバーに接続できます。この機能は、ウェブベースの MCP サーバーとの連携に特に便利です。

HTTP 接続機能の使用例を示します:

import asyncio
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from mcp_use import MCPAgent, MCPClient

async def main():
    """Run the example using a configuration file."""
    # Load environment variables
    load_dotenv()

    config = {
        "mcpServers": {
            "http": {
                "url": "http://localhost:8931/sse"
            }
        }
    }

    # Create MCPClient from config file
    client = MCPClient.from_dict(config)

    # Create LLM
    llm = ChatOpenAI(model="gpt-4o")

    # Create agent with the client
    agent = MCPAgent(llm=llm, client=client, max_steps=30)

    # Run the query
    result = await agent.run(
        "Find the best restaurant in San Francisco USING GOOGLE SEARCH",
        max_steps=30,
    )
    print(f"\nResult: {result}")

if __name__ == "__main__":
    # Run the appropriate example
    asyncio.run(main())

この例では、特定の HTTP ポートで稼働する MCP サーバーへの接続方法を示します。この例を実行する前に、MCP サーバーを起動してください。

マルチサーバーサポート

MCP-Use は複数の MCP サーバーを同時に利用できるため、異なるサーバーのツールを 1 つのエージェントに組み合わせられます。ウェブ閲覧とファイル操作、または 3D モデリングを組み合わせるなど、複数の機能を必要とする複雑なタスクに便利です。

設定

設定ファイルで複数のサーバーを構成できます:

{
  "mcpServers": {
    "airbnb": {
      "command": "npx",
      "args": ["-y", "@openbnb/mcp-server-airbnb", "--ignore-robots-txt"]
    },
    "playwright": {
      "command": "npx",
      "args": ["@playwright/mcp@latest"],
      "env": {
        "DISPLAY": ":1"
      }
    }
  }
}

使用方法

MCPClient クラスには、複数のサーバーを管理するためのメソッドがいくつか用意されています:

import asyncio
from mcp_use import MCPClient, MCPAgent
from langchain_anthropic import ChatAnthropic

async def main():
    # Create client with multiple servers
    client = MCPClient.from_config_file("multi_server_config.json")

    # Create agent with the client
    agent = MCPAgent(
        llm=ChatAnthropic(model="claude-3-5-sonnet-20240620"),
        client=client
    )

    try:
        # Run a query that uses tools from multiple servers
        result = await agent.run(
            "Search for a nice place to stay in Barcelona on Airbnb, "
            "then use Google to find nearby restaurants and attractions."
        )
        print(result)
    finally:
        # Clean up all sessions
        await client.close_all_sessions()

if __name__ == "__main__":
    asyncio.run(main())

ツールアクセス制御

MCP-Use では、エージェントが利用できるツールを制限して、エージェントの機能に対するセキュリティと制御を強化できます。

import asyncio
from mcp_use import MCPAgent, MCPClient
from langchain_openai import ChatOpenAI

async def main():
    # Create client
    client = MCPClient.from_config_file("config.json")

    # Create agent with restricted tools
    agent = MCPAgent(
        llm=ChatOpenAI(model="gpt-4"),
        client=client,
        disallowed_tools=["file_system", "network"]  # Restrict potentially dangerous tools
    )

    # Run a query with restricted tool access
    result = await agent.run(
        "Find the best restaurant in San Francisco"
    )
    print(result)

    # Clean up
    await client.close_all_sessions()

if __name__ == "__main__":
    asyncio.run(main())

ロードマップ

  • [x] 複数のサーバーを同時に使用
  • [x] リモートコネクターのテスト(http、ws)
  • [ ] ...

コントリビューション

コントリビューションを歓迎します!バグや機能リクエストがあれば、お気軽に issue を作成してください。

要件

  • Python 3.11+
  • MCP の実装(Playwright MCP など)
  • LangChain と適切なモデルライブラリ(OpenAI、Anthropic など)

引用

研究やプロジェクトで MCP-Use を使用する場合は、以下の形式で引用してください。

@software{mcp_use2025,
  author = {Zullo, Pietro},
  title = {MCP-Use: MCP Library for Python},
  year = {2025},
  publisher = {GitHub},
  url = {https://github.com/pietrozullo/mcp-use}
}

ライセンス

MIT

コメント

MCP は初めてですか?

プロトコルの概要から最初のサーバーの導入まで、よくある疑問にまとめて答えます。

AI アプリケーションが外部のツールやデータソースに統一的な方法で接続するためのオープンなプロトコルです。クライアントはモデルとの対話を担い、MCP サーバーはツール・リソース・プロンプトテンプレートを提供します。標準メッセージでやり取りするため、1 つのサーバーを複数のクライアントで再利用できます。