mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-26 15:34:18 +02:00
Rename McpError to MCPError and convert construction sites
This commit is contained in:
parent
2f49f9f7c0
commit
fdac9f9f2e
39 changed files with 217 additions and 281 deletions
|
|
@ -5,8 +5,8 @@ from typing import Any
|
|||
|
||||
import mcp_types as mt
|
||||
import pytest
|
||||
from mcp import McpError
|
||||
from mcp_types import ErrorData, TextContent
|
||||
from mcp import MCPError
|
||||
from mcp_types import TextContent
|
||||
|
||||
from fastmcp import Client, FastMCP
|
||||
from fastmcp.server.middleware import CallNext, Middleware, MiddlewareContext
|
||||
|
|
@ -299,7 +299,7 @@ async def test_middleware_can_access_initialize_result():
|
|||
|
||||
|
||||
async def test_middleware_mcp_error_during_initialization():
|
||||
"""Test that McpError raised in middleware during initialization is sent to client."""
|
||||
"""Test that MCPError raised in middleware during initialization is sent to client."""
|
||||
server = FastMCP("TestServer")
|
||||
|
||||
class ErrorThrowingMiddleware(Middleware):
|
||||
|
|
@ -308,15 +308,13 @@ async def test_middleware_mcp_error_during_initialization():
|
|||
context: MiddlewareContext[mt.InitializeRequest],
|
||||
call_next: CallNext[mt.InitializeRequest, mt.InitializeResult | None],
|
||||
) -> mt.InitializeResult | None:
|
||||
raise McpError(
|
||||
ErrorData(
|
||||
code=mt.INVALID_PARAMS, message="Invalid initialization parameters"
|
||||
)
|
||||
raise MCPError(
|
||||
code=mt.INVALID_PARAMS, message="Invalid initialization parameters"
|
||||
)
|
||||
|
||||
server.add_middleware(ErrorThrowingMiddleware())
|
||||
|
||||
with pytest.raises(McpError) as exc_info:
|
||||
with pytest.raises(MCPError) as exc_info:
|
||||
async with Client(server):
|
||||
pass
|
||||
|
||||
|
|
@ -325,7 +323,7 @@ async def test_middleware_mcp_error_during_initialization():
|
|||
|
||||
|
||||
async def test_middleware_mcp_error_before_call_next():
|
||||
"""Test McpError raised before calling next middleware."""
|
||||
"""Test MCPError raised before calling next middleware."""
|
||||
server = FastMCP("TestServer")
|
||||
|
||||
class EarlyErrorMiddleware(Middleware):
|
||||
|
|
@ -334,13 +332,11 @@ async def test_middleware_mcp_error_before_call_next():
|
|||
context: MiddlewareContext[mt.InitializeRequest],
|
||||
call_next: CallNext[mt.InitializeRequest, mt.InitializeResult | None],
|
||||
) -> mt.InitializeResult | None:
|
||||
raise McpError(
|
||||
ErrorData(code=mt.INVALID_REQUEST, message="Request validation failed")
|
||||
)
|
||||
raise MCPError(code=mt.INVALID_REQUEST, message="Request validation failed")
|
||||
|
||||
server.add_middleware(EarlyErrorMiddleware())
|
||||
|
||||
with pytest.raises(McpError) as exc_info:
|
||||
with pytest.raises(MCPError) as exc_info:
|
||||
async with Client(server):
|
||||
pass
|
||||
|
||||
|
|
@ -349,7 +345,7 @@ async def test_middleware_mcp_error_before_call_next():
|
|||
|
||||
|
||||
async def test_middleware_mcp_error_after_call_next():
|
||||
"""Test that McpError raised after call_next doesn't break the connection.
|
||||
"""Test that MCPError raised after call_next doesn't break the connection.
|
||||
|
||||
When an error is raised after call_next, the responder has already completed,
|
||||
so the error is caught but not sent to the responder (checked via _completed flag).
|
||||
|
|
@ -368,9 +364,7 @@ async def test_middleware_mcp_error_after_call_next():
|
|||
) -> mt.InitializeResult | None:
|
||||
await call_next(context)
|
||||
self.error_raised = True
|
||||
raise McpError(
|
||||
ErrorData(code=mt.INTERNAL_ERROR, message="Post-processing failed")
|
||||
)
|
||||
raise MCPError(code=mt.INTERNAL_ERROR, message="Post-processing failed")
|
||||
|
||||
middleware = PostProcessingErrorMiddleware()
|
||||
server.add_middleware(middleware)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue