Neural Mastery
← Practice
Correlate JSON-RPC Response to Pending Requesteasy

Correlate JSON-RPC Response to Pending Request

easy Agents in Production 15 min

NumPy allowed · plain Python earns 10 bonus points

Implement request/response correlation by id, the real mechanism an MCP client uses to route an async response back to the code that's waiting for it.

Task

Implement correlate_response(pending_requests, response_id). pending_requests maps request id -> the method name that was called. Return the method name for response_id, or 'unknown' if response_id isn't a pending request.

Function Signature

correlate_response(pending_requests: dict[str, str], response_id: str) -> str

Examples

Example 1: known pending request
Input: pending_requests = {'1': 'tools/call', '2': 'resources/read'} response_id = '1'
Output: 'tools/call'
Example 2: unknown response id
Input: pending_requests = {'1': 'tools/call'} response_id = '99'
Output: 'unknown'
Example 3: single pending request
Input: pending_requests = {'a': 'ping'} response_id = 'a'
Output: 'ping'

Constraints

  • pending_requests is a dict
Python3Saved ✓
def correlate_response(pending_requests, response_id):
# Your implementation here
pass

Case 1: known pending request
Input: pending_requests = {'1': 'tools/call', '2': 'resources/read'} response_id = '1'
Expected: 'tools/call'
Case 2: unknown response id
Input: pending_requests = {'1': 'tools/call'} response_id = '99'
Expected: 'unknown'
Case 3: single pending request
Input: pending_requests = {'a': 'ping'} response_id = 'a'
Expected: 'ping'