Skip to content

feat(McpAsyncClient): Handle ping request #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from

Conversation

OrGivati
Copy link
Contributor

@OrGivati OrGivati commented Apr 25, 2025

Add an handler for ping request in MCP client

Motivation and Context

According to the spec, the client and server must respond to ping with an empty map.
The server already handles ping requests.
I noticed this behavior is missing when I tested local running FastMCP server against the spring-ai MCP module.
The application failed to load with this error:

Error [ERR_UNHANDLED_ERROR]: Unhandled error. ({
Message received:   error: McpError: MCP error -32601: Method not found: ping

Note:
According to the spec, ping should be handled even when the server didn't send initialized message or the client didn't got initialize request.
From that reason, by default I am always adding the ping handler to the client.

How Has This Been Tested?

Unit test added

Breaking Changes

No breaking changes.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Closes #246

`getRequestURI()` → full request path (without query).

`getPathInfo()` → extra path after the servlet's mapping, or null if none.

So if I am configuring my Servlet to be mapped on `/somePath` and initiating the Servlet:
```
new HttpServletSseServerTransport(new ObjectMapper(), "/somePath/message", "/somePath/sse")
```

it will work and won't fail.
`getPathInfo` will return "/message" and "/sse".
while `getRequestURI` will return "/somePath/message" and "/somePath/sse" and will not fail on the validation.
# Conflicts:
#	mcp/src/main/java/io/modelcontextprotocol/server/transport/HttpServletSseServerTransportProvider.java
tzolov pushed a commit that referenced this pull request Jun 27, 2025
- Add ping request handler that responds with empty Map instead of null
- Ensure compliance with MCP protocol requirement for ping responses
- Add test for ping message request handling

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
@tzolov
Copy link
Contributor

tzolov commented Jun 27, 2025

@OrGivati , thank you for catching this. We've overlooked the client ping requirements.

@tzolov tzolov added this to the 0.11.0 milestone Jun 27, 2025
@tzolov tzolov self-assigned this Jun 27, 2025
@tzolov tzolov added the client label Jun 27, 2025
@tzolov
Copy link
Contributor

tzolov commented Jun 27, 2025

Rebased, cherry-picked, squashed, (minor cleaning) and merged at ebb9764

@tzolov tzolov closed this Jun 27, 2025
tzolov added a commit that referenced this pull request Jun 28, 2025
- Add ping() method to McpAsyncServerExchange that sends ping requests to clients
- Add ping() method to McpSyncServerExchange as synchronous wrapper
- Add OBJECT_TYPE_REF constant for ping response type handling
- Create McpSyncServerExchangeTests.java with full test coverage
- Add ping integration tests across WebFlux, WebMVC, and HttpServlet transports

Follow up of #203

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ping implementation to MCP client
2 participants