Skip to content

Commit e2b7b05

Browse files
docs(mcp.md): document new openapi to mcp tool support
1 parent d921df1 commit e2b7b05

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed

docs/my-website/docs/mcp.md

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,201 @@ litellm_settings:
246246
</TabItem>
247247
</Tabs>
248248

249+
## Converting OpenAPI Specs to MCP Servers
250+
251+
LiteLLM can automatically convert OpenAPI specifications into MCP servers, allowing you to expose any REST API as MCP tools. This is useful when you have existing APIs with OpenAPI/Swagger documentation and want to make them available as MCP tools.
252+
253+
### Benefits
254+
255+
- **Rapid Integration**: Convert existing APIs to MCP tools without writing custom MCP server code
256+
- **Automatic Tool Generation**: LiteLLM automatically generates MCP tools from your OpenAPI spec
257+
- **Unified Interface**: Use the same MCP interface for both native MCP servers and OpenAPI-based APIs
258+
- **Easy Testing**: Test and iterate on API integrations quickly
259+
260+
### Configuration
261+
262+
Add your OpenAPI-based MCP server to your `config.yaml`:
263+
264+
```yaml title="config.yaml - OpenAPI to MCP" showLineNumbers
265+
model_list:
266+
- model_name: gpt-4o
267+
litellm_params:
268+
model: openai/gpt-4o
269+
api_key: sk-xxxxxxx
270+
271+
mcp_servers:
272+
# OpenAPI Spec Example - Petstore API
273+
petstore_mcp:
274+
url: "https://petstore.swagger.io/v2"
275+
spec_path: "/path/to/openapi.json"
276+
auth_type: "none"
277+
278+
# OpenAPI Spec with API Key Authentication
279+
my_api_mcp:
280+
url: "http://0.0.0.0:8090"
281+
spec_path: "/path/to/openapi.json"
282+
auth_type: "api_key"
283+
auth_value: "your-api-key-here"
284+
285+
# OpenAPI Spec with Bearer Token
286+
secured_api_mcp:
287+
url: "https://api.example.com"
288+
spec_path: "/path/to/openapi.json"
289+
auth_type: "bearer_token"
290+
auth_value: "your-bearer-token"
291+
```
292+
293+
### Configuration Parameters
294+
295+
| Parameter | Required | Description |
296+
|-----------|----------|-------------|
297+
| `url` | Yes | The base URL of your API endpoint |
298+
| `spec_path` | Yes | Path or URL to your OpenAPI specification file (JSON or YAML) |
299+
| `auth_type` | No | Authentication type: `none`, `api_key`, `bearer_token`, `basic`, `authorization` |
300+
| `auth_value` | No | Authentication value (required if `auth_type` is set) |
301+
| `description` | No | Optional description for the MCP server |
302+
| `allowed_tools` | No | List of specific tools to allow (see [MCP Tool Filtering](#mcp-tool-filtering)) |
303+
| `disallowed_tools` | No | List of specific tools to block (see [MCP Tool Filtering](#mcp-tool-filtering)) |
304+
305+
### Usage Example
306+
307+
Once configured, you can use the OpenAPI-based MCP server just like any other MCP server:
308+
309+
<Tabs>
310+
<TabItem value="fastmcp" label="Python FastMCP">
311+
312+
```python title="Using OpenAPI-based MCP Server" showLineNumbers
313+
from fastmcp import Client
314+
import asyncio
315+
316+
# Standard MCP configuration
317+
config = {
318+
"mcpServers": {
319+
"petstore": {
320+
"url": "http://localhost:4000/petstore_mcp/mcp",
321+
"headers": {
322+
"x-litellm-api-key": "Bearer sk-1234"
323+
}
324+
}
325+
}
326+
}
327+
328+
# Create a client that connects to the server
329+
client = Client(config)
330+
331+
async def main():
332+
async with client:
333+
# List available tools generated from OpenAPI spec
334+
tools = await client.list_tools()
335+
print(f"Available tools: {[tool.name for tool in tools]}")
336+
337+
# Example: Get a pet by ID (from Petstore API)
338+
response = await client.call_tool(
339+
name="getpetbyid",
340+
arguments={"petId": "1"}
341+
)
342+
print(f"Response:\n{response}\n")
343+
344+
# Example: Find pets by status
345+
response = await client.call_tool(
346+
name="findpetsbystatus",
347+
arguments={"status": "available"}
348+
)
349+
print(f"Response:\n{response}\n")
350+
351+
if __name__ == "__main__":
352+
asyncio.run(main())
353+
```
354+
355+
</TabItem>
356+
357+
<TabItem value="cursor" label="Cursor IDE">
358+
359+
```json title="Cursor MCP Configuration for OpenAPI Server" showLineNumbers
360+
{
361+
"mcpServers": {
362+
"Petstore": {
363+
"url": "http://localhost:4000/petstore_mcp/mcp",
364+
"headers": {
365+
"x-litellm-api-key": "Bearer $LITELLM_API_KEY"
366+
}
367+
}
368+
}
369+
}
370+
```
371+
372+
</TabItem>
373+
374+
<TabItem value="openai" label="OpenAI Responses API">
375+
376+
```bash title="Using OpenAPI MCP Server with OpenAI" showLineNumbers
377+
curl --location 'https://api.openai.com/v1/responses' \
378+
--header 'Content-Type: application/json' \
379+
--header "Authorization: Bearer $OPENAI_API_KEY" \
380+
--data '{
381+
"model": "gpt-4o",
382+
"tools": [
383+
{
384+
"type": "mcp",
385+
"server_label": "petstore",
386+
"server_url": "http://localhost:4000/petstore_mcp/mcp",
387+
"require_approval": "never",
388+
"headers": {
389+
"x-litellm-api-key": "Bearer YOUR_LITELLM_API_KEY"
390+
}
391+
}
392+
],
393+
"input": "Find all available pets in the petstore",
394+
"tool_choice": "required"
395+
}'
396+
```
397+
398+
</TabItem>
399+
</Tabs>
400+
401+
### How It Works
402+
403+
1. **Spec Loading**: LiteLLM loads your OpenAPI specification from the provided `spec_path`
404+
2. **Tool Generation**: Each API endpoint in the spec becomes an MCP tool
405+
3. **Parameter Mapping**: OpenAPI parameters are automatically mapped to MCP tool parameters
406+
4. **Request Handling**: When a tool is called, LiteLLM converts the MCP request to the appropriate HTTP request
407+
5. **Response Translation**: API responses are converted back to MCP format
408+
409+
### OpenAPI Spec Requirements
410+
411+
Your OpenAPI specification should follow standard OpenAPI/Swagger conventions:
412+
- **Supported versions**: OpenAPI 3.0.x, OpenAPI 3.1.x, Swagger 2.0
413+
- **Required fields**: `paths`, `info` sections should be properly defined
414+
- **Operation IDs**: Each operation should have a unique `operationId` (this becomes the tool name)
415+
- **Parameters**: Request parameters should be properly documented with types and descriptions
416+
417+
### Example OpenAPI Spec Structure
418+
419+
```yaml title="sample-openapi.yaml" showLineNumbers
420+
openapi: 3.0.0
421+
info:
422+
title: My API
423+
version: 1.0.0
424+
paths:
425+
/pets/{petId}:
426+
get:
427+
operationId: getPetById
428+
summary: Get a pet by ID
429+
parameters:
430+
- name: petId
431+
in: path
432+
required: true
433+
schema:
434+
type: integer
435+
responses:
436+
'200':
437+
description: Successful response
438+
content:
439+
application/json:
440+
schema:
441+
type: object
442+
```
443+
249444
## MCP Tool Filtering
250445

251446
Control which tools are available from your MCP servers. You can either allow only specific tools or block dangerous ones.

0 commit comments

Comments
 (0)