Skip to main content

Using mcp-use with Anthropic

The Anthropic adapter allows you to seamlessly integrate tools, resources, and prompts from any MCP server with the Anthropic Python SDK. This enables you to use mcp-use as a comprehensive tool provider for your Anthropic-powered agents.

How it Works

The AnthropicMCPAdapter converts not only tools but also resources and prompts from your active MCP servers into a format compatible with Anthropic’s tool-calling feature. It maps each of these MCP constructs to a callable function that the Anthropic model can request.
  • Tools are converted directly to Anthropic functions.
  • Resources are converted into functions that take no arguments and read the resource’s content.
  • Prompts are converted into functions that accept the prompt’s arguments.
The adapter maintains a mapping of these generated functions to their actual execution logic, allowing you to easily call them when requested by the model.

Step-by-Step Guide

Here’s how to use the adapter to provide MCP tools, resources, and prompts to an Anthropic Chat Completion.
Before starting, install the Anthropic SDK:
1
First, set up your MCPClient with the desired MCP servers. This part of the process is the same as any other mcp-use application.
2
Next, instantiate the AnthropicMCPAdapter. This adapter will be responsible for converting MCP constructs into a format Anthropic can understand.
You can pass a disallowed_tools list to the adapter’s constructor to prevent specific tools, resources, or prompts from being exposed to the model.
3
Use the create_all method on the adapter to inspect all connected MCP servers and generate a list of tools, resources and prompts in the Anthropic function-calling format.
This list will include functions generated from your MCP tools, resources, and prompts.
If you don’t want to create all tools, you can call single functions. For example, if you only want to use tools and resources, you can do the following:
4
Now, you can use the generated anthropic_tools in a call to the Anthropic API. The model will use the descriptions of these tools to decide if it needs to call any of them to answer the user’s query.
5
If the model decides to use one or more tools, the response.stop_reason will be tool_use. You need to iterate through the tool use content blocks, execute the corresponding functions, and append the results to your message history.The AnthropicMCPAdapter makes this easy by providing a tool_executors dictionary and a parse_result method.
The adapter.parse_result(tool_result) method simplifies the process by correctly formatting the output, whether it’s from a standard tool, a resource, or a prompt.
6
Finally, send the updated message history which now includes the tool call results back to the model. This allows the model to use the information gathered from the tools to formulate its final answer.

Complete Example

For reference, here is the complete, runnable code for integrating mcp-use with the Anthropic SDK.