Neural Mastery
← Practice
MCP Server Tool Handlermedium

MCP Server Tool Handler

mediumAgents & Applications⏱ 15 min
Libraries allowed · Pure Python earns +10 bonus XP
🎯 Mission
Implement an MCP Server tool execution handler with parameter validation and structured result output.

Task

Implement `handle_tool_call` to execute registered functions and return standard MCP `{"content": [...], "isError": bool}` format.

Function Signature

handle_tool_call(tool_name: str, arguments: dict, registered_tools: dict) -> dict

Examples

Example 1: Valid Execution
Input: {"tool_name":"add","arguments":{"a":5,"b":3},"registered_tools":{"add":{"required":["a","b"]}}}
Output: {"content":[{"type":"text","text":"8"}],"isError":false}

Constraints

  • Return {"content": [{"type": "text", "text": str(res)}], "isError": False} on success.
  • Return {"content": [{"type": "text", "text": "error message"}], "isError": True} on missing parameters or exceptions.
Python3Saved ✓
def handle_tool_call(tool_name, arguments, registered_tools):
"""
Executes registered MCP server tool and returns standard MCP content response dictionary.
"""
# Your implementation here
pass

Case 1: Valid Execution
Input: {"tool_name":"add","arguments":{"a":5,"b":3},"registered_tools":{"add":{"required":["a","b"]}}}
Expected: {"content":[{"type":"text","text":"8"}],"isError":false}