pydantic-ai icon indicating copy to clipboard operation
pydantic-ai copied to clipboard

Toolsets, OpenAPI and Model Context Protocol

Open samuelcolvin opened this issue 1 year ago • 3 comments

TL;DR I spoke to @dsp on Friday, and the plan is to add support for MCP to PydanticAI via something I'm currently calling "toolsets".

One of the things I've realised while building PydanticAI is that building tools to give agents access to resources is going to be quite resource intensive and repetative — say you want to build an agent to interact with slack, 90% of your time will be spend figuring out the slack API and dealing with auth and reties, not building the agent.

I therefore propose the following process to make this kind of thing easy with PydanticAI:

  1. Add the concept of reusable "Toolsets" which contain multiple tools, their own state (e.g. http connection) and their own relation to agent dependencies
  2. Add a Toolset to support OpenAPI scheme APIs - e.g. so you could register an API which has an OpenAPI schema in a few lines of code - this should cover many common cases where you don't need full bidirectional communications
  3. Add support for registering MCP toolsets
  4. Create our own toolsets for interacting with common tools like slack, gmail, search etc. - this should be simple since I think many of these are already build for MCP, see here

samuelcolvin avatar Dec 02 '24 11:12 samuelcolvin

Was instantly thinking of this as I saw the release. I would love to help with this if you're looking for some extra hands!

skylarbpayne avatar Dec 03 '24 07:12 skylarbpayne

Hello! Just stumbled over this, I´m working on a project which incoroporates many of the same ideas, but in a different way. It´s basically a backend for both mcp servers as well as pydantic-ai agents. Perhaps it´s interesting for you. :)

https://github.com/phil65/LLMling

phil65 avatar Dec 10 '24 22:12 phil65

This framework has a very nice collection of tools and keeps everything quite simple. Only a few of them are API related but a lot of them seem to be quite useful:

https://docs.griptape.ai/stable/griptape-tools/

arjuna-dev avatar Jan 07 '25 15:01 arjuna-dev

Hi Everyone, excited to see where this goes. The link between OpenAPI, Pydantic, FastAPI is so well established you don't need me to explain it! MCP is looking like the equivalent of OpenAPI for this world, so naturally we're starting to familiar looking libraries like this...

Using PydanticAI today against a well documented OpenAPI endpoint one has to essentially re-implement tools for each of the endpoints one has spent time documenting, validating and implementing on the FastAPI side. It would be really cool to imagine a future where one could:

  • Point PydanticAI at an openapi.json definition and it would automatically register the discovered endpoints as tools. Today, the decorator only syntax doesn't really lend itself to this nicely.
  • Point PydanticAI to an instantiated FastAPI app which has much more information than just the OpenAPI schema available (e.g. more complex model/field validators, docstrings that Griffle can scoop up etc).

datajoely avatar Jan 13 '25 11:01 datajoely

+1 for Model Context Protocol support as their open-source tooling has grown quite a bit over the past 60 days

Toolsets can also assist in dealing with third-party data which is often raw and unstructured

slavakurilyak avatar Jan 26 '25 02:01 slavakurilyak

I've started an experimental implementation of MCP tools. It currently is a little hack-y right now and depends on #881

Unfortunately the official mcp python library requires 3.10+ which is incompatible with with pydantic's support for 3.9 -- I was too lazy to look into fixing the UV errors so i just bumped a bunch of stuff to 3.10, but for the official release we probably want to do something different

https://github.com/jonchun/pydantic-ai/tree/mcp

The meat of the change is in _pydantic.mcp_function_schema() that generates a function schema based off an MCP tool https://github.com/pydantic/pydantic-ai/compare/main...jonchun:pydantic-ai:mcp

jonchun avatar Feb 10 '25 06:02 jonchun

I've started an experimental implementation of MCP tools. It currently is a little hack-y right now and depends on #881

Unfortunately the official mcp python library requires 3.10+ which is incompatible with 3.9 -- I was too lazy to look into fixing the UV errors so i just bumped a bunch of stuff to 3.10, but for the official release we probably want to do something different

I can look into what it takes to use 3.9. We rely on some features like pattern matching and new union syntax , but if needed I think we can manage to downgrade

dsp avatar Feb 10 '25 08:02 dsp

@dsp - to be clear pydantic-ai already supports 3.9 and the problem is mcp (new dependency) only supports 3.10+. The issue would be not downgrading but rather supporting the mcp package on 3.9 (or choosing to drop support for 3.9 in pydantic ai)

jonchun avatar Feb 10 '25 08:02 jonchun

@dsp - to be clear pydantic-ai already supports 3.9 and the problem is mcp (new dependency) only supports 3.10+. The issue would be not downgrading but rather supporting the mcp package on 3.9 (or choosing to drop support for 3.9 in pydantic ai)

Sorry. I am the maintainer of mcp, I meant 3.9 in the mcp sdk.

Notable 3.9 is going to EOL in October, so we would support it only for 8 months

dsp avatar Feb 10 '25 10:02 dsp

@dsp - That makes a lot more sense, thanks!

Bringing Python 3.9 support into MCP seems like unnecessary effort, especially since it’s reaching EOL in October. Instead, it might be worth exploring what it would take to drop 3.9 support for pydantic-ai. Maybe MCP support could be handled as a separate package for now and merged later when the time is right?

jonchun avatar Feb 10 '25 10:02 jonchun

I vote for killing 3.9 now 💀

datajoely avatar Feb 10 '25 10:02 datajoely

@dsp - That makes a lot more sense, thanks!

Bringing Python 3.9 support into MCP seems like unnecessary effort, especially since it’s reaching EOL in October. Instead, it might be worth exploring what it would take to drop 3.9 support for pydantic-ai. Maybe MCP support could be handled as a separate package for now and merged later when the time is right?

That's one option. Selfishly I would love to say way more agents build around MCP so for I would be interested to make it as easy as possible to use MCP for Pydantic AI users, even if that means lowering the Python version on the MCP side.

dsp avatar Feb 10 '25 11:02 dsp

https://pypistats.org/packages/pydantic-ai

According to this, there's a decent number of 3.9 downloads (~1000/day) -- No idea how accurate these numbers are though.

jonchun avatar Feb 10 '25 11:02 jonchun

  • Point PydanticAI at an openapi.json definition and it would automatically register the discovered endpoints as tools. Today, the decorator only syntax doesn't really lend itself to this nicely.

@dsp and @datajoely not sure if you you've already seen this, but thought it'd be of interest. openapi-mcp-server is meant to create an MCP on top of an existing OpenAPI spec.

Also, a couple more written in Python:

https://github.com/baryhuang/mcp-server-any-openapi

https://github.com/pierrebrunelle/mcp-server-openai

mike-luabase avatar Mar 01 '25 14:03 mike-luabase

@mike-luabase I hadn't! That's cool, will check it out

I still think it would be an excellent move for Pydantic to be the leaders in this space in the same way they've unlocked the fastapi ecosystem

datajoely avatar Mar 02 '25 10:03 datajoely

Hello, I saw that there is an mcp-support branch, just wanted to say 1) thanks so much for all the great work! and 2) any ideas when we might see mcp-support in main? Thanks!

Tagging @Kludex since he seems to be the one working on this.

gitanon112 avatar Mar 14 '25 02:03 gitanon112

It's pending @samuelcolvin review.

Kludex avatar Mar 15 '25 07:03 Kludex

  • Please watch https://github.com/pydantic/pydantic-ai/issues/1268 for updates regarding the MCP Server features.

Kludex avatar Apr 01 '25 12:04 Kludex