magentic icon indicating copy to clipboard operation
magentic copied to clipboard

MCP Integration/Interaction

Open kmheckel opened this issue 11 months ago • 3 comments

Hi Jack,

Wondering what your thoughts are for writing programs with both magentic and MCP? I've been toying with both for a personal project.

I've gotten an example working where I essentially "import" the remote MCP tools which looks roughly like this in psuedocode:

` mcp_functions = []

for tool in MCP tools: name, signature, docs = tool

async def mcp_func(**kwargs):
    return await mcp_tool_call(name, kwargs)

mcp_func.__name__ = name
mcp_func.__doc__ = docs
mcp_func.__signature__ = signature

mcp_functions.append(mcp_func)    

`

While this works, it's somewhat ugly compared to if prompt functions could take Callables or the JSON schema provided back from MCP?

being able to input the schema would require a number of changes library wide though, and would likely pose issues for mixing synchronous local functions with asynchronous remote MCP functions.

Perhaps just adding some additional tooling to make this MCP resolution process more streamlined is the way to go?

kmheckel avatar Apr 02 '25 12:04 kmheckel

Hi! I haven't thought much about what integrating MCP would look like. Naively I hope this could be a new mcp_servers parameter to prompt, Chat, etc. Similar to how pydantic-ai make it work https://ai.pydantic.dev/mcp/client/ - looks like this requires a context manager async with agent.run_mcp_servers() but maybe could handle that inside the prompt-function.

jackmpcollins avatar Apr 07 '25 06:04 jackmpcollins

FastMCP v2 has a nice Client capability that should make it easy to talk to MCP servers without needing to deeply integrate into Magentic:

https://github.com/jlowin/fastmcp?tab=readme-ov-file#client-methods

kmheckel avatar Apr 16 '25 01:04 kmheckel

@jackmpcollins We're interested in this functionality as well. Do you think it's something that will likely be supported in the near future?

ashwin153 avatar Jul 11 '25 17:07 ashwin153