From 31c9245824678edfe4d4aeba3ecf6be28f5794e2 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Wed, 21 May 2025 14:42:00 -0700 Subject: [PATCH] feat: add MCP to responses API --- litellm/types/llms/openai.py | 5 +- .../test_openai_responses_api.py | 51 ++++++++++++++++++- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/litellm/types/llms/openai.py b/litellm/types/llms/openai.py index 0d880a4b1cf..8bb7d7d664e 100644 --- a/litellm/types/llms/openai.py +++ b/litellm/types/llms/openai.py @@ -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] diff --git a/tests/llm_responses_api_testing/test_openai_responses_api.py b/tests/llm_responses_api_testing/test_openai_responses_api.py index 333f99f9d46..4c3998e0c31 100644 --- a/tests/llm_responses_api_testing/test_openai_responses_api.py +++ b/tests/llm_responses_api_testing/test_openai_responses_api.py @@ -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." - \ No newline at end of file + + + +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) + +