Skip to content

Conversation

@ekzhu
Copy link
Contributor

@ekzhu ekzhu commented Apr 16, 2025

Resolves #6232, #6198

This PR introduces an optional parameter session to mcp_server_tools to support reuse of the same session.

import asyncio

from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.conditions import TextMentionTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_ext.tools.mcp import StdioServerParams, create_mcp_server_session, mcp_server_tools


async def main() -> None:
    model_client = OpenAIChatCompletionClient(model="gpt-4o", parallel_tool_calls=False)  # type: ignore
    params = StdioServerParams(
        command="npx",
        args=["@playwright/mcp@latest"],
        read_timeout_seconds=60,
    )
    async with create_mcp_server_session(params) as session:
        await session.initialize()
        tools = await mcp_server_tools(server_params=params, session=session)
        print(f"Tools: {[tool.name for tool in tools]}")

        agent = AssistantAgent(
            name="Assistant",
            model_client=model_client,
            tools=tools,  # type: ignore
        )

        termination = TextMentionTermination("TERMINATE")
        team = RoundRobinGroupChat([agent], termination_condition=termination)
        await Console(
            team.run_stream(
                task="Go to https://ekzhu.com/, visit the first link in the page, then tell me about the linked page."
            )
        )


asyncio.run(main())

Based on discussion in this thread: #6284, we will consider serialization and deserialization of MCP server tools when used in this manner in a separate issue.

This PR also replaces the json_schema_to_pydantic dependency with built-in utils.

@ekzhu ekzhu requested a review from victordibia April 16, 2025 23:37
@codecov
Copy link

codecov bot commented Apr 16, 2025

Codecov Report

Attention: Patch coverage is 90.24390% with 4 lines in your changes missing coverage. Please review.

Project coverage is 77.56%. Comparing base (844de21) to head (dd34581).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...ges/autogen-ext/src/autogen_ext/tools/mcp/_base.py 90.90% 2 Missing ⚠️
...n-core/src/autogen_core/utils/_json_to_pydantic.py 75.00% 1 Missing ⚠️
.../autogen-ext/src/autogen_ext/tools/mcp/_factory.py 85.71% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6312      +/-   ##
==========================================
+ Coverage   77.49%   77.56%   +0.07%     
==========================================
  Files         202      202              
  Lines       14742    14754      +12     
==========================================
+ Hits        11424    11444      +20     
+ Misses       3318     3310       -8     
Flag Coverage Δ
unittests 77.56% <90.24%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ekzhu ekzhu merged commit 27b834f into main Apr 17, 2025
59 checks passed
@ekzhu ekzhu deleted the ekzhu-share-session-mcp branch April 17, 2025 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Github MCP tools Doesn't work for write operations with Autogen

3 participants