Model Context Protocol (MCP) makes it easy to connect Claude Desktop or Cursor to your local tools and APIs. However, poorly constrained MCP tools can crash stdio streams or trigger infinite retry loops.
1. Never Crash the Stdio Transport
In stdio MCP servers, uncaught exceptions or stray print() statements to stdout corrupt JSON-RPC framing. Always trap errors inside tools and return structured strings:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("SafeToolServer")
@mcp.tool()
def query_records(keyword: str, limit: int = 10) -> str:
safe_limit = min(max(1, limit), 50)
try:
# Execute bounded read-only operation
return f"Found records for '{keyword}' (max {safe_limit})"
except Exception as exc:
return f"Tool error: {type(exc).__name__}"
2. Enforce Output Bounds & Read-Only Defaults
Clamp result limits (max 50) so a single tool response doesn't blow up the LLM context window in one turn.
Want Production-Ready .cursorrules & Boilerplates?
I'm a student developer in Japan funding my upcoming new life next spring by packaging strict, battle-tested AI coding setups:
-
Free Starter
.cursorrules& Reviewer Prompts ($0+): Download Free Starter Pack (or GitHub Repo) - Production AI Agent & MCP Suite ($19): Get Stack Kit
- All 10 Production Stacks Bundled ($99): 10-in-1 Ultimate Developer Suite
Top comments (0)