feat: add MCP to responses API

This commit is contained in:
Ishaan Jaff 2025-05-21 14:42:00 -07:00
parent b6508731dc
commit 31c9245824
2 changed files with 51 additions and 5 deletions

View file

@ -999,9 +999,8 @@ class ResponsesAPIResponse(BaseLiteLLMOpenAIResponseObject):
metadata: Optional[Dict]
model: Optional[str]
object: Optional[str]
output: Union[
List[ResponseOutputItem],
List[Union[GenericResponseOutputItem, OutputFunctionToolCall]],
output: List[
Union[GenericResponseOutputItem, OutputFunctionToolCall, ResponseOutputItem]
]
parallel_tool_calls: bool
temperature: Optional[float]

View file

@ -2,7 +2,7 @@ import os
import sys
import pytest
import asyncio
from typing import Optional
from typing import Optional, cast
from unittest.mock import patch, AsyncMock
sys.path.insert(0, os.path.abspath("../.."))
@ -1032,4 +1032,51 @@ def test_basic_computer_use_preview_tool_call():
# Validate the input format
assert isinstance(request_body["input"], str)
assert request_body["input"] == "Check the latest OpenAI news on bing.com."
def test_mcp_tools_with_responses_api():
litellm._turn_on_debug()
MCP_TOOLS = [
{
"type": "mcp",
"server_label": "deepwiki",
"server_url": "https://mcp.deepwiki.com/mcp",
"allowed_tools": ["ask_question"]
}
]
MODEL = "openai/gpt-4.1"
USER_QUERY = "What transport protocols does the 2025-03-26 version of the MCP spec (modelcontextprotocol/modelcontextprotocol) support?"
#########################################################
# Step 1: OpenAI will use MCP LIST, and return a list of MCP calls for our approval
response = litellm.responses(
model=MODEL,
tools=MCP_TOOLS,
input=USER_QUERY
)
print(response)
response = cast(ResponsesAPIResponse, response)
mcp_approval_id: Optional[str]
for output in response.output:
if output.type == "mcp_approval_request":
mcp_approval_id = output.id
break
# Step 2: Send followup with approval for the MCP call
response_with_mcp_call = litellm.responses(
model=MODEL,
tools=MCP_TOOLS,
input=[
{
"type": "mcp_approval_response",
"approve": True,
"approval_request_id": mcp_approval_id
}
],
previous_response_id=response.id,
)
print(response_with_mcp_call)