Using MCP Servers with OpenRouter
Use MCP Servers with OpenRouter
MCP servers are a popular way of providing LLMs with tool calling abilities, and are an alternative to using OpenAI-compatible tool calling.
By converting MCP (Anthropic) tool definitions to OpenAI-compatible tool definitions, you can use MCP servers with OpenRouter.
In this example, we’ll use Anthropic’s MCP client SDK to interact with the File System MCP, all with OpenRouter under the hood.
First, some setup. In order to run this you will need to pip install the packages, and create a .env
file with OPENAI_API_KEY set. This example also assumes the directory /Applications
exists.
Next, our helper function to convert MCP tool definitions to OpenAI tool definitions:
And, the MCP client itself; a regrettable ~100 lines of code. Note that the SERVER_CONFIG is hard-coded into the client, but of course could be parameterized for other MCP servers.
Assembling all of the above code into mcp-client.py, you get a client that behaves as follows (some outputs truncated for brevity):