Skip to content

Commit e9df629

Browse files
committed
refactor: make on_ping non-optional with default handler per MCP spec
1 parent fa9c59b commit e9df629

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/mcp/server/lowlevel/server.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ async def lifespan(_: Server[LifespanResultT]) -> AsyncIterator[dict[str, Any]]:
9797
yield {}
9898

9999

100+
async def _ping_handler(ctx: ServerRequestContext[Any], params: types.RequestParams | None) -> types.EmptyResult:
101+
return types.EmptyResult()
102+
103+
100104
class Server(Generic[LifespanResultT]):
101105
def __init__(
102106
self,
@@ -171,8 +175,7 @@ def __init__(
171175
on_ping: Callable[
172176
[ServerRequestContext[LifespanResultT], types.RequestParams | None],
173177
Awaitable[types.EmptyResult],
174-
]
175-
| None = None,
178+
] = _ping_handler,
176179
# Notification handlers
177180
on_roots_list_changed: Callable[
178181
[ServerRequestContext[LifespanResultT], types.NotificationParams | None],
@@ -223,10 +226,6 @@ def __init__(
223226
}
224227
)
225228

226-
# Default ping handler if not provided
227-
if "ping" not in self._request_handlers:
228-
self._request_handlers["ping"] = _ping_handler
229-
230229
self._notification_handlers.update(
231230
{
232231
method: handler
@@ -648,5 +647,3 @@ def streamable_http_app(
648647
)
649648

650649

651-
async def _ping_handler(ctx: ServerRequestContext[Any], params: types.RequestParams | None) -> types.EmptyResult:
652-
return types.EmptyResult()

0 commit comments

Comments
 (0)