litellm/tests/mcp_tests/mcp_e2e_upstream_server.py
Yuneng Jiang 9f79a59a41
test(mcp): migrate the mcp test helpers to the mcp 2.x MCPServer API
pyproject pins mcp>=2.2.0,<3, where FastMCP was renamed to MCPServer and
stateless_http, json_response, transport_security, host and port moved off
the constructor onto streamable_http_app() and run(). The two helpers still
imported mcp.server.fastmcp, so every module reaching them died at collection
and integration-extensions went red on the release candidate.

This ports the helpers verbatim from main, where the same migration already
landed in 479360ae87 after rc/1.103.0 was cut. Only the two helper files are
taken; the rest of that commit restores unrelated test surface.

Collection on the four affected modules: 4 errors and 0 tests before, 0
errors and 6 tests after.
2026-09-19 19:09:46 -07:00

40 lines
1.1 KiB
Python

"""Deterministic upstream MCP server for the mcp e2e suite.
A tiny MCP server exposing `add` and `multiply` over streamable-http so the
suite has a self-hosted, offline upstream to register and exercise. DNS-rebinding
protection is turned off because the litellm container reaches this over the
compose network by service name (`mcp-upstream:8090`), not localhost, and the
stack is an isolated throwaway. Bind host/port come from MCP_HOST/MCP_PORT.
"""
import os
from mcp.server.mcpserver import MCPServer
from mcp.server.transport_security import TransportSecuritySettings
mcp: MCPServer = MCPServer("e2e-math")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two integers"""
return a + b
@mcp.tool()
def multiply(a: int, b: int) -> int:
"""Multiply two integers"""
return a * b
def main() -> None:
mcp.run(
transport="streamable-http",
host=os.getenv("MCP_HOST", "0.0.0.0"),
port=int(os.getenv("MCP_PORT", "8090")),
transport_security=TransportSecuritySettings(enable_dns_rebinding_protection=False),
)
if __name__ == "__main__":
main()