Use 120 characters instead of 88 (#856)

This commit is contained in:
Marcelo Trylesinski
2025-06-11 02:45:50 -07:00
committed by GitHub
parent f7265f7b91
commit 543961968c
90 changed files with 687 additions and 2142 deletions

View File

@@ -130,11 +130,7 @@ def make_everything_fastmcp() -> FastMCP:
# Request sampling from the client
result = await ctx.session.create_message(
messages=[
SamplingMessage(
role="user", content=TextContent(type="text", text=prompt)
)
],
messages=[SamplingMessage(role="user", content=TextContent(type="text", text=prompt))],
max_tokens=100,
temperature=0.7,
)
@@ -278,11 +274,7 @@ def make_fastmcp_stateless_http_app():
def run_server(server_port: int) -> None:
"""Run the server."""
_, app = make_fastmcp_app()
server = uvicorn.Server(
config=uvicorn.Config(
app=app, host="127.0.0.1", port=server_port, log_level="error"
)
)
server = uvicorn.Server(config=uvicorn.Config(app=app, host="127.0.0.1", port=server_port, log_level="error"))
print(f"Starting server on port {server_port}")
server.run()
@@ -290,11 +282,7 @@ def run_server(server_port: int) -> None:
def run_everything_legacy_sse_http_server(server_port: int) -> None:
"""Run the comprehensive server with all features."""
_, app = make_everything_fastmcp_app()
server = uvicorn.Server(
config=uvicorn.Config(
app=app, host="127.0.0.1", port=server_port, log_level="error"
)
)
server = uvicorn.Server(config=uvicorn.Config(app=app, host="127.0.0.1", port=server_port, log_level="error"))
print(f"Starting comprehensive server on port {server_port}")
server.run()
@@ -302,11 +290,7 @@ def run_everything_legacy_sse_http_server(server_port: int) -> None:
def run_streamable_http_server(server_port: int) -> None:
"""Run the StreamableHTTP server."""
_, app = make_fastmcp_streamable_http_app()
server = uvicorn.Server(
config=uvicorn.Config(
app=app, host="127.0.0.1", port=server_port, log_level="error"
)
)
server = uvicorn.Server(config=uvicorn.Config(app=app, host="127.0.0.1", port=server_port, log_level="error"))
print(f"Starting StreamableHTTP server on port {server_port}")
server.run()
@@ -314,11 +298,7 @@ def run_streamable_http_server(server_port: int) -> None:
def run_everything_server(server_port: int) -> None:
"""Run the comprehensive StreamableHTTP server with all features."""
_, app = make_everything_fastmcp_streamable_http_app()
server = uvicorn.Server(
config=uvicorn.Config(
app=app, host="127.0.0.1", port=server_port, log_level="error"
)
)
server = uvicorn.Server(config=uvicorn.Config(app=app, host="127.0.0.1", port=server_port, log_level="error"))
print(f"Starting comprehensive StreamableHTTP server on port {server_port}")
server.run()
@@ -326,11 +306,7 @@ def run_everything_server(server_port: int) -> None:
def run_stateless_http_server(server_port: int) -> None:
"""Run the stateless StreamableHTTP server."""
_, app = make_fastmcp_stateless_http_app()
server = uvicorn.Server(
config=uvicorn.Config(
app=app, host="127.0.0.1", port=server_port, log_level="error"
)
)
server = uvicorn.Server(config=uvicorn.Config(app=app, host="127.0.0.1", port=server_port, log_level="error"))
print(f"Starting stateless StreamableHTTP server on port {server_port}")
server.run()
@@ -369,9 +345,7 @@ def server(server_port: int) -> Generator[None, None, None]:
@pytest.fixture()
def streamable_http_server(http_server_port: int) -> Generator[None, None, None]:
"""Start the StreamableHTTP server in a separate process."""
proc = multiprocessing.Process(
target=run_streamable_http_server, args=(http_server_port,), daemon=True
)
proc = multiprocessing.Process(target=run_streamable_http_server, args=(http_server_port,), daemon=True)
print("Starting StreamableHTTP server process")
proc.start()
@@ -388,9 +362,7 @@ def streamable_http_server(http_server_port: int) -> Generator[None, None, None]
time.sleep(0.1)
attempt += 1
else:
raise RuntimeError(
f"StreamableHTTP server failed to start after {max_attempts} attempts"
)
raise RuntimeError(f"StreamableHTTP server failed to start after {max_attempts} attempts")
yield
@@ -427,9 +399,7 @@ def stateless_http_server(
time.sleep(0.1)
attempt += 1
else:
raise RuntimeError(
f"Stateless server failed to start after {max_attempts} attempts"
)
raise RuntimeError(f"Stateless server failed to start after {max_attempts} attempts")
yield
@@ -459,9 +429,7 @@ async def test_fastmcp_without_auth(server: None, server_url: str) -> None:
@pytest.mark.anyio
async def test_fastmcp_streamable_http(
streamable_http_server: None, http_server_url: str
) -> None:
async def test_fastmcp_streamable_http(streamable_http_server: None, http_server_url: str) -> None:
"""Test that FastMCP works with StreamableHTTP transport."""
# Connect to the server using StreamableHTTP
async with streamablehttp_client(http_server_url + "/mcp") as (
@@ -484,9 +452,7 @@ async def test_fastmcp_streamable_http(
@pytest.mark.anyio
async def test_fastmcp_stateless_streamable_http(
stateless_http_server: None, stateless_http_server_url: str
) -> None:
async def test_fastmcp_stateless_streamable_http(stateless_http_server: None, stateless_http_server_url: str) -> None:
"""Test that FastMCP works with stateless StreamableHTTP transport."""
# Connect to the server using StreamableHTTP
async with streamablehttp_client(stateless_http_server_url + "/mcp") as (
@@ -562,9 +528,7 @@ def everything_server(everything_server_port: int) -> Generator[None, None, None
time.sleep(0.1)
attempt += 1
else:
raise RuntimeError(
f"Comprehensive server failed to start after {max_attempts} attempts"
)
raise RuntimeError(f"Comprehensive server failed to start after {max_attempts} attempts")
yield
@@ -601,10 +565,7 @@ def everything_streamable_http_server(
time.sleep(0.1)
attempt += 1
else:
raise RuntimeError(
f"Comprehensive StreamableHTTP server failed to start after "
f"{max_attempts} attempts"
)
raise RuntimeError(f"Comprehensive StreamableHTTP server failed to start after " f"{max_attempts} attempts")
yield
@@ -648,9 +609,7 @@ class NotificationCollector:
await self.handle_tool_list_changed(message.root.params)
async def call_all_mcp_features(
session: ClientSession, collector: NotificationCollector
) -> None:
async def call_all_mcp_features(session: ClientSession, collector: NotificationCollector) -> None:
"""
Test all MCP features using the provided session.
@@ -680,9 +639,7 @@ async def call_all_mcp_features(
# Test progress callback functionality
progress_updates = []
async def progress_callback(
progress: float, total: float | None, message: str | None
) -> None:
async def progress_callback(progress: float, total: float | None, message: str | None) -> None:
"""Collect progress updates for testing (async version)."""
progress_updates.append((progress, total, message))
print(f"Progress: {progress}/{total} - {message}")
@@ -726,19 +683,12 @@ async def call_all_mcp_features(
# Verify we received log messages from the sampling tool
assert len(collector.log_messages) > 0
assert any(
"Requesting sampling for prompt" in msg.data for msg in collector.log_messages
)
assert any(
"Received sampling result from model" in msg.data
for msg in collector.log_messages
)
assert any("Requesting sampling for prompt" in msg.data for msg in collector.log_messages)
assert any("Received sampling result from model" in msg.data for msg in collector.log_messages)
# 4. Test notification tool
notification_message = "test_notifications"
notification_result = await session.call_tool(
"notification_tool", {"message": notification_message}
)
notification_result = await session.call_tool("notification_tool", {"message": notification_message})
assert len(notification_result.content) == 1
assert isinstance(notification_result.content[0], TextContent)
assert "Sent notifications and logs" in notification_result.content[0].text
@@ -773,36 +723,24 @@ async def call_all_mcp_features(
# 2. Dynamic resource
resource_category = "test"
dynamic_content = await session.read_resource(
AnyUrl(f"resource://dynamic/{resource_category}")
)
dynamic_content = await session.read_resource(AnyUrl(f"resource://dynamic/{resource_category}"))
assert isinstance(dynamic_content, ReadResourceResult)
assert len(dynamic_content.contents) == 1
assert isinstance(dynamic_content.contents[0], TextResourceContents)
assert (
f"Dynamic resource content for category: {resource_category}"
in dynamic_content.contents[0].text
)
assert f"Dynamic resource content for category: {resource_category}" in dynamic_content.contents[0].text
# 3. Template resource
resource_id = "456"
template_content = await session.read_resource(
AnyUrl(f"resource://template/{resource_id}/data")
)
template_content = await session.read_resource(AnyUrl(f"resource://template/{resource_id}/data"))
assert isinstance(template_content, ReadResourceResult)
assert len(template_content.contents) == 1
assert isinstance(template_content.contents[0], TextResourceContents)
assert (
f"Template resource data for ID: {resource_id}"
in template_content.contents[0].text
)
assert f"Template resource data for ID: {resource_id}" in template_content.contents[0].text
# Test prompts
# 1. Simple prompt
prompts = await session.list_prompts()
simple_prompt = next(
(p for p in prompts.prompts if p.name == "simple_prompt"), None
)
simple_prompt = next((p for p in prompts.prompts if p.name == "simple_prompt"), None)
assert simple_prompt is not None
prompt_topic = "AI"
@@ -812,16 +750,12 @@ async def call_all_mcp_features(
# The actual message structure depends on the prompt implementation
# 2. Complex prompt
complex_prompt = next(
(p for p in prompts.prompts if p.name == "complex_prompt"), None
)
complex_prompt = next((p for p in prompts.prompts if p.name == "complex_prompt"), None)
assert complex_prompt is not None
query = "What is AI?"
context = "technical"
complex_result = await session.get_prompt(
"complex_prompt", {"user_query": query, "context": context}
)
complex_result = await session.get_prompt("complex_prompt", {"user_query": query, "context": context})
assert isinstance(complex_result, GetPromptResult)
assert len(complex_result.messages) >= 1
@@ -837,9 +771,7 @@ async def call_all_mcp_features(
print(f"Received headers: {headers_data}")
# Test 6: Call tool that returns full context
context_result = await session.call_tool(
"echo_context", {"custom_request_id": "test-123"}
)
context_result = await session.call_tool("echo_context", {"custom_request_id": "test-123"})
assert len(context_result.content) == 1
assert isinstance(context_result.content[0], TextContent)
@@ -871,9 +803,7 @@ async def sampling_callback(
@pytest.mark.anyio
async def test_fastmcp_all_features_sse(
everything_server: None, everything_server_url: str
) -> None:
async def test_fastmcp_all_features_sse(everything_server: None, everything_server_url: str) -> None:
"""Test all MCP features work correctly with SSE transport."""
# Create notification collector