Skip to content

SmolAgents MCP Client #1200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Apr 18, 2025
Merged

SmolAgents MCP Client #1200

merged 25 commits into from
Apr 18, 2025

Conversation

grll
Copy link
Contributor

@grll grll commented Apr 15, 2025

Fixes #1179

As discussed in the issue above the idea is to introduce a new class: MCPClient allowing our users to manage the connection with one or several MCP servers.

It supports 2 syntaxes:

  1. with a context manager for a fully managed connection with the MCP server, similar to what ToolCollection.from_mcp is doing today.
  2. a new try / finally syntax which allow more fine grained control over the connection and its lifecycle via a new mcp_client instance.

Note 1: I have moved the .connect() inside the init of the MCPClient I am not sure it is desirable here. Initially I kept it outside even though it's one more call in my opinion it makes things more explicit while if you want less boilerplate you can always use the with syntax.

Note 2: I have added a couple of tests but documentation is missing, waiting for a confirmation of the design and then I can add.

cc: @aymeric-roucher / @albertvillanova

def __enter__(self) -> list[Tool]:
"""Connect to the MCP server and return the tools directly.

Note that because of the `.connect` in the init, the mcp_client
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't think it is better to do a connect here? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in my opinion it is better here and separately to be more explicit but the CrewAI folks told me they prefer to have it in the init so I thought it might be just me ;). Happy to make the change

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, let's see what consensus on this is.

My feeling is that connect is a "side-effect" and __init__ is not the place for things like that. It gives a client more freedom on what when to connect.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Member

@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for addressing this!

The implementation looks solid to me. I'd suggest merging it as is and gathering feedback from the community to iterate if needed (for the connect e.g.).

Just some CI errors to be fixed before merging.

grll and others added 4 commits April 17, 2025 13:04
Co-authored-by: Albert Villanova del Moral <8515462+albertvillanova@users.noreply.github.com>
Co-authored-by: Albert Villanova del Moral <8515462+albertvillanova@users.noreply.github.com>
Co-authored-by: Albert Villanova del Moral <8515462+albertvillanova@users.noreply.github.com>
Co-authored-by: Albert Villanova del Moral <8515462+albertvillanova@users.noreply.github.com>
@grll
Copy link
Contributor Author

grll commented Apr 17, 2025

@albertvillanova we might want to add some doc to advertise the capabilities somewhere?

Copy link
Member

@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, docs as well:

  • The reference may be added to reference/tools, under ToolCollection and before Agent Types
  • And may be some descriptive docs in tutorials/tools, at the end of the section Use a collection of tools

What do you think?

@albertvillanova
Copy link
Member

I can fix the quality issue if it is OK for you: you will need to pull before adding your changes.

@grll
Copy link
Contributor Author

grll commented Apr 17, 2025

I can have a shot at it later tonight I will pull your change before (I mean the doc)

Copy link
Member

@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments...


def __init__(
self,
serverparams: StdioServerParameters | dict[str, Any] | list[StdioServerParameters | dict[str, Any]],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not calling this server_params or server_parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems the official python sdk use server_parameters so we could go for that

@albertvillanova
Copy link
Member

Hi @grll, we're planning to do a release this morning.

Could you let me know if you'd prefer to wrap up this PR yourself, or if you'd like me to take care of the remaining changes? Just let me know what's most convenient for you.

grll and others added 2 commits April 18, 2025 10:16
Co-authored-by: Albert Villanova del Moral <8515462+albertvillanova@users.noreply.github.com>
Co-authored-by: Albert Villanova del Moral <8515462+albertvillanova@users.noreply.github.com>
@grll
Copy link
Contributor Author

grll commented Apr 18, 2025

Are you ok with adding the doc? Is there something else left?

@albertvillanova
Copy link
Member

albertvillanova commented Apr 18, 2025

I think it is the only missing thing...

EDIT: And fixing quality and tests.

Copy link
Member

@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this excellent contribution!

The implementation of the MCPClient looks great and aligns well with the goals discussed. I really appreciate the addition of thorough tests: they'll be very helpful for maintainability. Also, thank you for the constructive discussions throughout the process; they've helped shaping a solid design.

@albertvillanova albertvillanova merged commit ee0a0fb into huggingface:main Apr 18, 2025
4 checks passed
@grll
Copy link
Contributor Author

grll commented Apr 18, 2025

@albertvillanova thanks a lot for pushing it over the finish line! I have plenty of ideas to improve this further and bring a lot more MCP features in smolagents, I really like the way this is going with a Client class it will enable a lot of powerful things in the future :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change MCP tool import to make context manager optional
5 participants