
mcp trials
用于在一个客户端连接多个MCP服务器的工具,支持OpenAI函数调用和SSE通信。
Repository Info
About This Server
用于在一个客户端连接多个MCP服务器的工具,支持OpenAI函数调用和SSE通信。
Model Context Protocol (MCP) - This server can be integrated with AI applications to provide additional context and capabilities, enabling enhanced AI interactions and functionality.
Documentation
Connecting to multiple MCP servers in one client
- Find tool function meta datas
- Construct tools list for OpenAI chat API, e.g.
{
"type": "function",
"function": {
"name": "add_numbers",
"description": "Adds two numbers.",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "integer", "description": "First number"},
"b": {"type": "integer", "description": "Second number"},
},
"required": ["a", "b"],
},
},
},
- Call OpenAI chat/response API, with
toolsparam, andtool_choiceoptionally - Get response content and tool calls if it exists
msg = response.choices[0].message
resp, tool_calls = msg.content, msg.tool_calls
- Extract tool function name and tool function arguments from the tool calls
function_name = tool_call.function.name
function_args = tool_call.function.arguments
if function_args:
function_args = json.loads(function_args)
- Find MCP server session by function name.
SSE server URL may require sse path:
http://127.0.0.1:8081/sse
async def call_sse_server(self, function_name, arguments=None):
async with sse_client("http://127.0.0.1:8081/sse") as (sse_read, sse_write):
async with ClientSession(sse_read, sse_write) as sse_session:
# Initialize the SSE server connection
await sse_session.initialize()
# Call tools and resources on the SSE server
reverse_result = await sse_session.call_tool(
function_name, arguments=arguments
)
return reverse_result
Initialize server session, cache it, and close it gracefully on exit
- Extract tool function execution result from server
session.call_tool.
res = esult.content[0].text
Refactor MCPAgent, connect to MCP servers on class initialization, keep sessions in attribute sessions, keep function name to session mapping, close session on exit
Quick Start
Clone the repository
git clone https://github.com/cowardfxn/mcp-trialsInstall dependencies
cd mcp-trials
npm installFollow the documentation
Check the repository's README.md file for specific installation and usage instructions.
Repository Details
Recommended MCP Servers
Discord MCP
Enable AI assistants to seamlessly interact with Discord servers, channels, and messages.
Knit MCP
Connect AI agents to 200+ SaaS applications and automate workflows.
Apify MCP Server
Deploy and interact with Apify actors for web scraping and data extraction.
BrowserStack MCP
BrowserStack MCP Server for automated testing across multiple browsers.
Zapier MCP
A Zapier server that provides automation capabilities for various apps.