Skip to content

Fix JSON-RPC 2.0 notification handling in StreamableHttpServerWrapper#28

Merged
0xeb merged 2 commits intomainfrom
fix/jsonrpc-notification-handling
Jan 20, 2026
Merged

Fix JSON-RPC 2.0 notification handling in StreamableHttpServerWrapper#28
0xeb merged 2 commits intomainfrom
fix/jsonrpc-notification-handling

Conversation

@0xeb
Copy link
Copy Markdown
Owner

@0xeb 0xeb commented Jan 20, 2026

Summary

This PR fixes JSON-RPC 2.0 notification handling in StreamableHttpServerWrapper and adds a pre-commit hook for automatic formatting.

1. Fix: JSON-RPC 2.0 Notification Handling

According to JSON-RPC 2.0 specification section 4.1:

"A Notification is a Request object without an 'id' member... The Server MUST NOT reply to a Notification, including those that are within a batch request."

Previously, StreamableHttpServerWrapper would pass notifications to the handler and return whatever response it produced. For unknown notification methods like notifications/initialized, this resulted in error responses like:

{"error":{"code":-32601,"message":"Method 'notifications/initialized' not found"},"id":null,"jsonrpc":"2.0"}

This caused MCP clients (like OpenAI Codex CLI) to fail during handshaking.

The fix:

  • Detect notifications by checking for missing or null id field
  • Return 202 Accepted with no response body for notifications
  • Still call the handler so notification processing can occur

2. Pre-commit Hook for Automatic Formatting

Added .githooks/pre-commit that automatically formats staged C++ files with clang-format before commit.

To enable: git config core.hooksPath .githooks

This prevents formatting issues from reaching CI.

Test Plan

  • All existing tests pass (70/70)
  • New notification handling test passes
  • Verified fix works with OpenAI Codex CLI MCP client
  • Code formatted with clang-format

According to JSON-RPC 2.0 specification section 4.1:
"A Notification is a Request object without an 'id' member...
The Server MUST NOT reply to a Notification."

Previously, the StreamableHttpServerWrapper would pass notifications to
the handler and return whatever response it produced (typically an error
for unknown methods like 'notifications/initialized'). This caused MCP
clients like OpenAI Codex CLI to fail during handshaking.

This fix:
- Detects notifications by checking for missing/null 'id' field
- Returns 202 Accepted with no response body for notifications
- Still calls the handler so notification processing can occur
- Silently ignores any handler errors for notifications

Added test_notification_handling() to verify:
- notifications/initialized returns 202 with empty body
- notifications/cancelled returns 202 with empty body

Fixes compatibility with MCP clients that follow the JSON-RPC 2.0 spec.
@0xeb 0xeb force-pushed the fix/jsonrpc-notification-handling branch from 9a2cae1 to 879e6f6 Compare January 20, 2026 18:52
Adds .githooks/pre-commit that automatically formats staged C++ files
before commit. This prevents formatting issues from reaching CI.

To enable: git config core.hooksPath .githooks

The hook:
- Finds clang-format (prefers versioned like clang-format-19)
- Formats only staged .cpp/.hpp/.h/.c files
- Re-stages formatted files automatically
@0xeb 0xeb merged commit 0a492a2 into main Jan 20, 2026
6 of 7 checks passed
@0xeb 0xeb deleted the fix/jsonrpc-notification-handling branch January 20, 2026 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant