You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MCP server is open-source which allows you to access a wide variety of tools provided by 3rd parties and community members, reducing the need to write a bunch of functions.
However, MCP clients requires the code to run in async function and ReAct, even with asyncify, still can't run the tools as the forward function needs to call await on the tools as well.
Happy to contribute to this, but not sure where would be the best place to start.
Reference code:
# Create server parameters for stdio connectionserver_params=StdioServerParameters(
command="docker", # Executableargs=[
"run",
"-i",
"--rm",
"--mount",
"type=bind,src=/Users/kalanyuz/Github/test_project/playground,dst=/projects/playground",
"--mount",
"mcp/filesystem",
"/projects",
],
env=None, # Optional environment variables
)
asyncdefrun():
asyncwithstdio_client(server_params) as (read, write):
asyncwithClientSession(read, write) assession:
# Initialize the connectionawaitsession.initialize()
# List available prompts# prompts = await session.list_prompts()# Get a prompt# prompt = await session.get_prompt(# "example-prompt", arguments={"arg1": "value"}# )# List available resources# resources = await session.list_resources()# List available toolstools=awaitsession.list_tools()
# Read a resource# content, mime_type = await session.read_resource("file://system")# Call a toolresult=awaitsession.call_tool("list_directory", arguments={"path": "/projects/src"})
args, arg_types, arg_desc=map_json_schema_to_tool_args(tools.tools[2].inputSchema)
test_tool=dspy.Tool(
func=lambdapath, content: session.call_tool(tools.tools[2].name, {'path':path, 'content': content}),
name=tools.tools[2].name,
desc=tools.tools[2].description,
args=args,
arg_types=arg_types,
arg_desc=arg_desc
)
bot=dspy.ReAct('request -> output', tools=[test_tool])
result=awaitbot(request='Generate random message and write to /projects/playground/test.txt')
print(result)
if__name__=="__main__":
importasyncioasyncio.run(run())
And ReAct's updated forward that can call subroutine tools
To clarify, the blocking part is you want to call the tools asynchronously, while react's forward function is synchronous? Potentially using the @syncify annotation to convert tools calls to sync calls can resolve the issue? The caveat is you actually lose the benefits of async, but I am not sure if async tool calling is useful for ReAct, because you need the full obersavation before going to the next iteration. Let me know your thoughts!
I tried both of your suggestions with @syncify, either at the top of run, or wrapping the mcp'ssession.call_tool with it, but to no avail.
There might be some thread conflicts happening? It would throw an exception that the function needs to be called from an anyio worker thread, and supplying raise_sync_error flag would throw another error saying there's already an anyio thread running.
I could, however, use a combination of anyio.to_thread.run_sync with syncify to make this work without directly modifying ReAct's module. Not sure if there's going to be any performance impact to the module itself.
So with this, the code above would be changed to:
args, arg_types, arg_desc=map_json_schema_to_tool_args(tools.tools[2].inputSchema)
test_tool=dspy.Tool(
func=lambda**kwargs: syncify(session.call_tool)(tools.tools[2].name, kwargs),
name=tools.tools[2].name,
desc=tools.tools[2].description,
args=args,
arg_types=arg_types,
arg_desc=arg_desc,
)
bot=dspy.ReAct("request -> output", tools=[test_tool])
bot=functools.partial(bot, request="Generate random message and write to /projects/playground/test.txt")
bot=anyio.to_thread.run_sync(bot)
result=awaitbot
now without modifications to ReAct.
Not sure which would be the right path. I can contribute with a documentation on how to use mcp with dspy with the above logic if you think this is acceptable.
What feature would you like to see?
MCP server is open-source which allows you to access a wide variety of tools provided by 3rd parties and community members, reducing the need to write a bunch of functions.
However, MCP clients requires the code to run in async function and ReAct, even with asyncify, still can't run the tools as the forward function needs to call await on the tools as well.
Happy to contribute to this, but not sure where would be the best place to start.
Reference code:
And ReAct's updated
forward
that can call subroutine toolsWould you like to contribute?
Additional Context
No response
The text was updated successfully, but these errors were encountered: