Menu

6 lessons from building safe MCP servers for Claude
📰

6 lessons from building safe MCP servers for Claude

DEV Community: python·longdang2072·11 days ago
#3i1u23zt
#devtopython#devto#dev#claude#server
Long article (737 words) — no AI summary yet.
Reading 0:00
15s threshold

Connecting Claude to a database or an internal API through MCP (Model Context Protocol) takes about 20 lines of Python. Making it safe to leave connected takes a bit more thought. Here are six lessons from building MCP servers for SQL databases, REST APIs, Notion, Google Sheets, local documents and spreadsheets.

All examples use the official Python SDK 2.x.

1. Start from a minimal server

from mcp . server . mcpserver import MCPServer

mcp = MCPServer ( "crm-demo" )

@mcp . tool ( ) def find_customer ( query : str ) -> list [ dict ] : """Search customers by part of their name or email address.""" . . .

if __name__ == "__main__" : mcp . run ( )

Enter fullscreen mode

Exit fullscreen mode

Note: in SDK 2.x, FastMCP became MCPServer in mcp.server.mcpserver . Type hints and the docstring become the tool schema Claude sees.

2. Read-only by default, writes behind a switch

Don't just check permissions inside a write tool. Don't register it at all unless the user opted in:

ALLOW_WRITES = os . environ .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.