Skip to content

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 <[email protected]>
@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 <[email protected]>
tzolov added a commit that referenced this pull request Jul 1, 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 <[email protected]>
@tzolov tzolov mentioned this pull request Jul 22, 2025
@ZeaLotPhenix
Copy link

Thanks! I meet this issue too, i'm using the latest version of spring-ai : 1.0.0-M7, and it's using 0.9.0 sdk,i changed to 0.11.3,it works fine now!

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

3 participants