Spaces:
Sleeping
Sleeping
File size: 1,971 Bytes
01f0e50 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import mcp
from mcp.client.stdio import stdio_client
from mcp import StdioServerParameters
from agents import FunctionTool
import json
params = StdioServerParameters(command="uv", args=["run", "accounts_server.py"], env=None)
async def list_accounts_tools():
async with stdio_client(params) as streams:
async with mcp.ClientSession(*streams) as session:
await session.initialize()
tools_result = await session.list_tools()
return tools_result.tools
async def call_accounts_tool(tool_name, tool_args):
async with stdio_client(params) as streams:
async with mcp.ClientSession(*streams) as session:
await session.initialize()
result = await session.call_tool(tool_name, tool_args)
return result
async def read_accounts_resource(name):
async with stdio_client(params) as streams:
async with mcp.ClientSession(*streams) as session:
await session.initialize()
result = await session.read_resource(f"accounts://accounts_server/{name}")
return result.contents[0].text
async def read_strategy_resource(name):
async with stdio_client(params) as streams:
async with mcp.ClientSession(*streams) as session:
await session.initialize()
result = await session.read_resource(f"accounts://strategy/{name}")
return result.contents[0].text
async def get_accounts_tools_openai():
openai_tools = []
for tool in await list_accounts_tools():
schema = {**tool.inputSchema, "additionalProperties": False}
openai_tool = FunctionTool(
name=tool.name,
description=tool.description,
params_json_schema=schema,
on_invoke_tool=lambda ctx, args, toolname=tool.name: call_accounts_tool(toolname, json.loads(args))
)
openai_tools.append(openai_tool)
return openai_tools |