Skip to content

Commit

Permalink
Merge pull request #62 from modelcontextprotocol/davidsp/error-fixes
Browse files Browse the repository at this point in the history
Handle MCPErrors separately
  • Loading branch information
dsp-ant authored Nov 25, 2024
2 parents 5dfce75 + f7e47f3 commit 873f6bf
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/mcp/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ async def main():
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__)
Expand Down Expand Up @@ -393,8 +394,8 @@ async def run(
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,
Expand Down Expand Up @@ -429,6 +430,8 @@ async def run(
)
)
response = await handler(req)
except McpError as err:
response = err.error
except Exception as err:
if raise_exceptions:
raise err
Expand Down

0 comments on commit 873f6bf

Please sign in to comment.