Merge pull request #62 from modelcontextprotocol/davidsp/error-fixes

Handle MCPErrors separately
This commit is contained in:
David Soria Parra
2024-11-25 14:15:06 +00:00
committed by GitHub

View File

@@ -74,6 +74,7 @@ from mcp.server.models import InitializationOptions
from mcp.server.session import ServerSession
from mcp.server.stdio import stdio_server as stdio_server
from mcp.shared.context import RequestContext
from mcp.shared.exceptions import McpError
from mcp.shared.session import RequestResponder
logger = logging.getLogger(__name__)
@@ -393,8 +394,8 @@ class Server:
read_stream: MemoryObjectReceiveStream[types.JSONRPCMessage | Exception],
write_stream: MemoryObjectSendStream[types.JSONRPCMessage],
initialization_options: InitializationOptions,
# When True, exceptions are returned as messages to the client.
# When False, exceptions are raised, which will cause the server to shut down
# When False, exceptions are returned as messages to the client.
# When True, exceptions are raised, which will cause the server to shut down
# but also make tracing exceptions much easier during testing and when using
# in-process servers.
raise_exceptions: bool = False,
@@ -429,6 +430,8 @@ class Server:
)
)
response = await handler(req)
except McpError as err:
response = err.error
except Exception as err:
if raise_exceptions:
raise err