Skip to content

Commit d37182c

Browse files
hieutrtrtoreleonCopilot
authored
release 0.1.9 - HotFix and add Example (#28)
* Toreleon non config mcphub (#21) * Add CI/CD workflows for testing, version bumping, and PyPI publishing; enhance README with development and CI/CD details; implement comprehensive tests for MCPHub and MCPServers functionality. * Enhance CI workflow by installing all extras with Poetry * Refactor CI workflow to include version bumping and streamline package publishing using Poetry * Update README to clarify MCPHub installation options with framework-specific dependencies * Restrict pull request triggers to the release branch only * Update CI workflow to enable fail-fast strategy and restrict Python versions to 3.12 only * Refactor CI workflows to remove version bumping from CI and add tagging functionality; update README for clarity on automatic versioning and tagging process. * Refactor CI workflows to integrate version bumping and ensure proper versioning before publishing; update version-bump workflow to set outputs for new version. * Bump version to 0.1.6 in pyproject.toml * Refactor CI workflows: remove version-bump workflow and integrate version extraction and tagging directly in the publish job; update package version to 0.1.7 in pyproject.toml * Fix test data structure in TestMCPServersParams to match expected format Updated the mock data in the test for _load_predefined_servers_params to reflect the correct structure, ensuring that the "mcpServers" key wraps the predefined server details. This change enhances the accuracy of the test and aligns it with the actual implementation. * Toreleon cli (#22) * Add CI/CD workflows for testing, version bumping, and PyPI publishing; enhance README with development and CI/CD details; implement comprehensive tests for MCPHub and MCPServers functionality. * Enhance CI workflow by installing all extras with Poetry * Refactor CI workflow to include version bumping and streamline package publishing using Poetry * Update README to clarify MCPHub installation options with framework-specific dependencies * Restrict pull request triggers to the release branch only * Update CI workflow to enable fail-fast strategy and restrict Python versions to 3.12 only * Refactor CI workflows to remove version bumping from CI and add tagging functionality; update README for clarity on automatic versioning and tagging process. * Refactor CI workflows to integrate version bumping and ensure proper versioning before publishing; update version-bump workflow to set outputs for new version. * Bump version to 0.1.6 in pyproject.toml * Refactor CI workflows: remove version-bump workflow and integrate version extraction and tagging directly in the publish job; update package version to 0.1.7 in pyproject.toml * Fix test data structure in TestMCPServersParams to match expected format Updated the mock data in the test for _load_predefined_servers_params to reflect the correct structure, ensuring that the "mcpServers" key wraps the predefined server details. This change enhances the accuracy of the test and aligns it with the actual implementation. * Add CLI commands and utilities for MCPHub configuration management - Implemented CLI commands for initializing, adding, removing, and listing MCP servers. - Created utility functions for loading and saving configuration files. - Added tests for CLI commands to ensure proper functionality and error handling. * Enhance test for 'init' command to skip config creation when it exists * Enhance test for 'init' command to skip config creation when it exists (#23) * Enhance test for 'init' command to skip config creation when it exists * Update tests/test_cli.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Enhance test for 'init' command to skip config creation when it exists (#24) * Refactor dependencies in pyproject.toml to separate optional dependencies and clean up main dependencies section * Toreleon fix dependencies (#25) * Enhance test for 'init' command to skip config creation when it exists * Refactor dependencies in pyproject.toml to separate optional dependencies and clean up main dependencies section * Optional init (#26) * solve test * remove legacy functions * [hot-fix] autogen create_adapters and add examples --------- Co-authored-by: Thang Le <42534763+toreleon@users.noreply.github.com> Co-authored-by: toreleon <levietthang0512@outlook.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 0104477 commit d37182c

File tree

5 files changed

+115
-33
lines changed

5 files changed

+115
-33
lines changed

examples/with_autogen.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Example of using MCPHub with Autogen Agents.
3+
1. Initialize MCPHub to manage MCP servers
4+
2. Fetch MCP tools and adapters for Autogen
5+
3. Create and run an agent with MCP tools
6+
"""
7+
8+
import asyncio
9+
10+
from autogen_agentchat.agents import AssistantAgent
11+
from autogen_agentchat.ui import Console
12+
from autogen_core import CancellationToken
13+
from autogen_ext.models.openai import OpenAIChatCompletionClient
14+
from mcphub import MCPHub
15+
16+
17+
async def main():
18+
# Initialize MCPHub - automatically loads .mcphub.json and sets up servers
19+
hub = MCPHub()
20+
21+
# Fetch MCP tools adapted for Autogen
22+
tool_adapters = await hub.fetch_autogen_mcp_adapters("azure-storage-mcp")
23+
model_client = OpenAIChatCompletionClient(model="gpt-4")
24+
25+
# Create and run agent with MCP tools
26+
complex_task = """Please help me analyze the following complex problem:
27+
We need to design a new feature for our product that balances user privacy
28+
with data collection for improving the service. Consider the ethical implications,
29+
technical feasibility, and business impact. Break down your thinking process
30+
step by step, and provide a detailed recommendation with clear justification
31+
for each decision point."""
32+
agent = AssistantAgent(
33+
name="assistant",
34+
model_client=model_client,
35+
tools=tool_adapters,
36+
system_message="You are a helpful assistant.",
37+
)
38+
39+
await Console(
40+
agent.run_stream(task=complex_task, cancellation_token=CancellationToken())
41+
)
42+
43+
if __name__ == "__main__":
44+
# Run the async main function
45+
asyncio.run(main())

examples/with_langchain.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Example of using MCPHub with LangChain Agents.
3+
1. Initialize MCPHub to manage MCP servers
4+
2. Fetch MCP tools for LangChain
5+
3. Create and run an agent with MCP tools
6+
"""
7+
8+
import asyncio
9+
import json
10+
11+
from langchain_mcp_adapters.tools import load_mcp_tools
12+
from langchain_openai import ChatOpenAI
13+
from langgraph.prebuilt import create_react_agent
14+
from mcp import ClientSession, StdioServerParameters
15+
from mcp.client.stdio import stdio_client
16+
from mcphub import MCPHub
17+
18+
model = ChatOpenAI(model="gpt-4o")
19+
20+
async def main():
21+
# Initialize MCPHub - automatically loads .mcphub.json and sets up servers
22+
hub = MCPHub()
23+
24+
# Fetch MCP tools for LangChain
25+
tools = await hub.fetch_langchain_mcp_tools("azure-storage-mcp")
26+
tools_dict = [
27+
{"name": tool.name, "description": tool.description, "args_schema": tool.args_schema} for tool in tools
28+
]
29+
print("Available MCP Tools:")
30+
print(json.dumps(tools_dict, indent=2))
31+
32+
# Create and run agent with MCP tools
33+
complex_task = """Please help me analyze the following complex problem:
34+
We need to design a new feature for our product that balances user privacy
35+
with data collection for improving the service. Consider the ethical implications,
36+
technical feasibility, and business impact. Break down your thinking process
37+
step by step, and provide a detailed recommendation with clear justification
38+
for each decision point."""
39+
agent = create_react_agent(model, tools)
40+
agent_response = await agent.ainvoke({"messages": complex_task})
41+
print("\nAgent Response:")
42+
print(agent_response.get("messages")[1].content)
43+
44+
if __name__ == "__main__":
45+
asyncio.run(main())
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,51 @@
1+
"""
2+
Example of using MCPHub with OpenAI Agents.
3+
1. Initialize MCPHub to manage MCP servers
4+
2. Fetch an MCP server with async context manager
5+
3. List available tools from the server
6+
4. Create and run an agent with MCP tools
7+
"""
8+
19
import asyncio
210
import json
311
from agents import Agent, Runner
412
from mcphub import MCPHub
513

614
async def main():
7-
"""
8-
Example of using MCPHub to integrate MCP servers with OpenAI Agents.
9-
10-
This example demonstrates:
11-
1. Initializing MCPHub
12-
2. Fetching and using an MCP server
13-
3. Listing available tools
14-
4. Creating and running an agent with MCP tools
15-
"""
16-
17-
# Step 1: Initialize MCPHub
18-
# MCPHub will automatically:
19-
# - Find .mcphub.json in your project
20-
# - Load server configurations
21-
# - Set up servers (clone repos, run setup scripts if needed)
15+
# Initialize MCPHub - automatically loads .mcphub.json and sets up servers
2216
hub = MCPHub()
2317

24-
# Step 2: Create an MCP server instance using async context manager
25-
# Parameters:
26-
# - mcp_name: The name of the server from your .mcphub.json
27-
# - cache_tools_list: Cache the tools list for better performance
18+
# Fetch MCP server - handles server setup and tool caching
2819
async with hub.fetch_openai_mcp_server(
2920
mcp_name="sequential-thinking-mcp",
3021
cache_tools_list=True
3122
) as server:
32-
# Step 3: List available tools from the MCP server
33-
# This shows what capabilities are available to your agent
23+
# Get available tools from the server
3424
tools = await server.list_tools()
35-
36-
# Pretty print the tools for better readability
3725
tools_dict = [
3826
dict(tool) if hasattr(tool, "__dict__") else tool for tool in tools
3927
]
4028
print("Available MCP Tools:")
4129
print(json.dumps(tools_dict, indent=2))
4230

43-
# Step 4: Create an OpenAI Agent with MCP server
44-
# The agent can now use all tools provided by the MCP server
31+
# Create agent with MCP server integration
4532
agent = Agent(
4633
name="Assistant",
4734
instructions="Use the available tools to accomplish the given task",
48-
mcp_servers=[server] # Provide the MCP server to the agent
35+
mcp_servers=[server]
4936
)
5037

51-
# Step 5: Run your agent with a complex task
52-
# The agent will automatically have access to all MCP tools
38+
# Run agent with a task
5339
complex_task = """Please help me analyze the following complex problem:
5440
We need to design a new feature for our product that balances user privacy
5541
with data collection for improving the service. Consider the ethical implications,
5642
technical feasibility, and business impact. Break down your thinking process
5743
step by step, and provide a detailed recommendation with clear justification
5844
for each decision point."""
5945

60-
# Execute the task and get the result
6146
result = await Runner.run(agent, complex_task)
6247
print("\nAgent Response:")
6348
print(result)
6449

6550
if __name__ == "__main__":
66-
# Run the async main function
6751
asyncio.run(main())

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "mcphub"
7-
version = "0.1.8"
7+
version = "0.1.9"
88
description = "A Python package for managing and integrating Model Context Protocol (MCP) servers with AI frameworks like OpenAI Agents, LangChain, and Autogen"
99
readme = "README.md"
1010
authors = [

src/mcphub/adapters/autogen.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
try:
22
from typing import List
33

4-
from autogen_ext.tools.mcp import StdioMcpToolAdapter
4+
from autogen_ext.tools.mcp import StdioMcpToolAdapter, StdioServerParams
55

66
from .base import MCPBaseAdapter
77

88
class MCPAutogenAdapter(MCPBaseAdapter):
99
async def create_adapters(self, mcp_name: str) -> List[StdioMcpToolAdapter]:
1010
server_params = self.get_server_params(mcp_name)
11+
12+
autogen_mcp_server_params = StdioServerParams(
13+
command=server_params.command,
14+
args=server_params.args,
15+
env=server_params.env,
16+
cwd=server_params.cwd
17+
)
18+
1119
async with self.create_session(mcp_name) as session:
1220
tools = await session.list_tools()
1321
return [
14-
await StdioMcpToolAdapter.from_server_params(server_params, tool.name)
22+
await StdioMcpToolAdapter.from_server_params(autogen_mcp_server_params, tool.name)
1523
for tool in tools.tools
1624
]
1725

0 commit comments

Comments
 (0)