Skip to content

feat: Enhance MCP integration and example #8060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 0 commits into from

Conversation

ThanabordeeN
Copy link

@ThanabordeeN ThanabordeeN commented Apr 12, 2025

📝 Changes Description

TLDR; Enhanced DSPy's ReAct with concurrent MCP server support, enabling efficient integration with external APIs (e.g., Airbnb) through asynchronous operations and robust resource management.

This MR/PR introduces significant improvements to DSPy's ReAct framework, enabling concurrent interaction with multiple MCP (Model Context Protocol) servers. Key enhancements include:

  • mcp_multi_server_example.py:
    • Provides a practical demonstration of concurrent MCP server operations within a ReAct workflow.
  • mcp.py:
    • Add mcp module intregrations
  • Asynchronous ReAct Operations:
    • Leverages ReAct's asynchronous capabilities (async_forward in react.py) for efficient management of multiple server connections.

Example Usage

import asyncio
import os
import dspy

# Define a simple DSPy Signature for the agent
class MultiServerSignature(dspy.Signature):
    """Perform operations using tools potentially available across multiple MCP servers."""
    request: str = dspy.InputField(desc="The user's request, potentially requiring external tools.")
    output: str = dspy.OutputField(desc="The final response to the user's request after potentially using tools.")

async def main():
    # Initialize language model
    lm = dspy.LM("gemini/gemini-2.0-flash", api_key=os.getenv("GOOGLE_API_KEY"))
    dspy.configure(lm=lm)
    
    # Configure MCP servers
    config_path = "path/to/servers_config.json"
    
    async with dspy.MCPServerManager() as server_manager:
        # Load and initialize servers from config
        config = server_manager.load_config(config_path)
        await server_manager.initialize_servers(config)
        
        # Get all tools from all connected servers
        all_mcp_tools = await server_manager.get_all_tools()
        
        # Create a ReAct agent with the tools
        react_agent = dspy.ReAct(
            MultiServerSignature,
            tools=all_mcp_tools,
            max_iters=7
        )
        
        # Execute a request
        result = await react_agent.async_forward(request="Your request here")
        print("Final Result:", result.output)

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

✅ Contributor Checklist

  • [-] Pre-Commit checks are passing (locally and remotely)

⚠️ Warnings

  • MCP tools are currently incompatible with combinations of default functions and MCP functions must use async function instead
  • Some MCP functions may exhibit unexpected behavior.
  • SSE MCP functionality has not been thoroughly tested.

@ThanabordeeN ThanabordeeN marked this pull request as ready for review April 12, 2025 10:16
@ThanabordeeN ThanabordeeN reopened this Apr 12, 2025
@ThanabordeeN ThanabordeeN changed the title feat: Enhance MCP integration with improved resource management and example updates feat: Enhance MCP integration Apr 12, 2025
@ThanabordeeN ThanabordeeN changed the title feat: Enhance MCP integration feat: Enhance MCP integration and example Apr 12, 2025
@ThanabordeeN ThanabordeeN marked this pull request as draft April 13, 2025 08:09
@ThanabordeeN ThanabordeeN marked this pull request as ready for review April 13, 2025 09:18
@TomeHirata TomeHirata self-requested a review April 14, 2025 04:21
pyproject.toml Outdated
@@ -41,10 +41,12 @@ dependencies = [
"json-repair>=0.30.0",
"tenacity>=8.2.3",
"anyio",
"mcp>=1.6.0",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't include mcp in the main dependency. Include it in the extra dependency instead

pyproject.toml Outdated
@@ -99,7 +101,7 @@ repository = "https://github.com/stanfordnlp/dspy"
keywords = ["dspy", "ai", "language models", "llm", "openai"]

[tool.poetry.dependencies]
python = ">=3.9,<3.13"
python = ">=3.10,<3.13"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change the minimum support Python version

@@ -0,0 +1,127 @@
# DSPy Model Context Protocol (MCP) Guide
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Examples are not actively used. Can we have a documentation page in docs/docs/tutorials?

@TomeHirata
Copy link
Collaborator

@ThanabordeeN Thank you for the PR. The maintainer team is discussing how we should support the MCP integration in dspy.ReAct. Can we keep this PR on hold while we discuss the design direction?

@ThanabordeeN
Copy link
Author

Okay , with pleasure.

@TomeHirata
Copy link
Collaborator

TomeHirata commented Apr 15, 2025

Hi, @ThanabordeeN. Regarding the MCP support in dspy.ReAct. Here is the proposal from the maintainer team. Are you willing to contribute?

  1. We provide a public util to convert mcp.types.Tool and mcp.client.session.ClientSession into dspy.primitives.tool.Tool so that MCP tools can be passed to dspy.ReAct
  2. dspy.ReAct should be able to use async callables and async Tools in the sync forward method. While we plan to provide an async forward method, it should be a follow-up after the MVP version
  3. We don't plan to support MCP server session management within the DSPy library in the short term. We believe it should be addressed by the MCP library side.

@ThanabordeeN
Copy link
Author

Of course, I am interested in being a part of this development because I have personally benefited from using this tool in research and other personal projects.

@okhat
Copy link
Collaborator

okhat commented Apr 15, 2025

Thank you @ThanabordeeN ! Looking forward to collaborating with you. (+ Thank you @TomeHirata !)

@okhat okhat self-assigned this Apr 16, 2025
@ThanabordeeN
Copy link
Author

Could you please inform me where this part is located, or confirm if it has not yet been released in any branch?

  1. We provide a public util to convert mcp.types.Tool and mcp.client.session.ClientSession into dspy.primitives.tool.Tool so that MCP tools can be passed to dspy.ReAct

@okhat
Copy link
Collaborator

okhat commented Apr 22, 2025

@ThanabordeeN That quote is a plan, not a statement about something that exists.

Read Tomu's post again, this is a list of proposals that this PR should adhere to.

  1. We should provide a public util to convert mcp.types.Tool and mcp.client.session.ClientSession into dspy.primitives.tool.Tool so that MCP tools can be passed to dspy.ReAct
  2. We should update dspy.ReAct sot i can use async callables and async Tools in the sync forward method. While we plan to provide an async forward method, it should be a follow-up after the MVP version (not this PR).
  3. We don't plan to support MCP server session management within the DSPy library in the short term. We believe it should be addressed by the MCP library side.

@ThanabordeeN
Copy link
Author

Thank you for the explanation.

1 similar comment
@ThanabordeeN
Copy link
Author

Thank you for the explanation.

@TomeHirata
Copy link
Collaborator

Hi, @ThanabordeeN. How's the progress so far? We got some requests for the MCP tool integration with ReAct, and want to ship the first version soon. If it would take time for you, would you mind us working on the development based on the idea in this PR? We can add you as a co-author of the PR.

@ThanabordeeN
Copy link
Author

Hi , @TomeHirata Thank you for your message and the proposal regarding the MCP tool integration with ReAct.
I am currently in the process of developing this part. I am very happy for the team to step in and help with the development based on the idea in the PR, and I would like to be a Co-author on this part as well.
To be transparent, I am currently working on this alongside research for another project, which might cause some delays.

However, I am keen to collaborate closely to ensure this integration is completed successfully.

@ThanabordeeN
Copy link
Author

@TomeHirata For cleaner commits, I'm going to open a new PR for the upcoming update.

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.

3 participants