The Problem: Claude is Smart but Lacks Your Local Context
Giving Claude architectural advice is great until it hallucinates a database column you deleted three weeks ago. I used to waste about 15 minutes every hour on the ‘copy-paste dance’—exporting DDLs, scrubbing sensitive rows, and pasting them into the chat. Every time the schema changed, the cycle repeated.
Manual workflows like this are brittle and dangerous. If you forget to update the context, the AI suggests queries for non-existent tables. Even worse, pasting internal data into a web UI creates a massive security headache. You need a secure way for the AI to ‘reach out’ and see your environment directly. The Model Context Protocol (MCP) solves this by creating a secure bridge between the LLM and your local machine.
How the Model Context Protocol Works
MCP is an open standard from Anthropic that lets AI models interact with external data sources. Think of it as a universal driver for LLMs. Instead of building a custom integration for every new AI tool, you build an MCP server once. Any compatible client, like Claude Desktop or IDEs, can then instantly use your tools.
The system relies on three moving parts:
- MCP Host: The interface you type in (like Claude Desktop).
- MCP Client: The hidden engine within the host that manages the connection.
- MCP Server: A lightweight program you write that exposes specific resources, such as a PostgreSQL database or a Jira API.
Most local servers communicate via standard input/output (stdio). Mastering this protocol moves AI beyond a simple chat interface. It transforms the model into a functional agent capable of navigating your actual development environment.
Step-by-Step: Building a SQLite MCP Server
We will build a Python server that allows Claude to query a local SQLite database. This enables Claude to answer questions like “Who were our top 10 customers by revenue last month?” by executing real SQL instead of guessing based on old snippets.
1. Prepare Your Environment
Ensure you have Python 3.10 or later. We’ll use a virtual environment to keep the installation clean and avoid dependency conflicts with other projects.
mkdir my-mcp-server
cd my-mcp-server
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install mcp sqlalchemy
2. Create the Server Logic
Create a file named server.py. We will use the FastMCP SDK, which handles the complex JSON-RPC communication for us. This tool will allow Claude to run read-only queries safely.
from mcp.server.fastmcp import FastMCP
import sqlite3
mcp = FastMCP("DataExplorer")
DB_PATH = "production_mirror.db"
@mcp.tool()
def query_database(sql: str) -> str:
"""Run a read-only SELECT query on the local database."""
try:
with sqlite3.connect(DB_PATH) as conn:
cursor = conn.cursor()
# Safety check: Prevent destructive commands
if not sql.strip().upper().startswith("SELECT"):
return "Error: Only SELECT queries are allowed for security."
cursor.execute(sql)
return str(cursor.fetchall())
except Exception as e:
return f"Database Error: {str(e)}"
if __name__ == "__main__":
mcp.run()
3. Linking Claude Desktop to Your Server
Claude Desktop needs explicit instructions to launch your script. You must modify its configuration file. On macOS, find it at ~/Library/Application Support/Claude/claude_desktop_config.json. Windows users should look in %APPDATA%\Claude\claude_desktop_config.json.
Update the file with the absolute paths to your Python executable and script:
{
"mcpServers": {
"my-sqlite-server": {
"command": "/path/to/my-mcp-server/.venv/bin/python",
"args": ["/path/to/my-mcp-server/server.py"]
}
}
}
Relative paths are the most common cause of failure here. Claude Desktop runs in a separate shell environment, so it won’t know where your project folder is unless you tell it exactly.
4. Verifying the Connection
Restart Claude Desktop completely to refresh the config. Look for a small hammer icon in the bottom right of the message box. If you see it, Claude has successfully registered your tool. Try asking: “Look at my local database and list the 5 most recent entries in the ‘logs’ table.”
Beyond Local Files: Connecting Private APIs
MCP handles more than just local databases. You can wrap internal company APIs that aren’t exposed to the public internet. For example, I recently built a server for a private inventory API. This allowed Claude to summarize stock levels without me ever leaving the chat interface.
The structure for an API tool is nearly identical to the database version:
import requests
@mcp.tool()
def check_inventory(sku: str) -> dict:
"""Checks stock levels for a specific SKU via internal API."""
response = requests.get(f"https://api.internal-tools.com/v1/stock/{sku}")
return response.json()
Hard-Won Best Practices
Building several of these servers taught me that debugging MCP can be tricky. These four tips will save you hours of frustration:
- Use the Inspector: Use the
mcp-inspectortool to test your server before trying it in Claude. It catches JSON formatting errors that Claude Desktop often hides. - Log to Files: Standard
print()statements will break the MCP connection because they interfere with the stdio stream. Use the Python logging library to write to adebug.logfile instead. - Enforce Read-Only: Never give an LLM
DROPorDELETEpermissions. Use a database user with restricted SELECT-only access to prevent AI-generated accidents. - Watch the Clock: Claude Desktop will time out if a tool takes longer than 30 seconds to respond. If your data is huge, use
LIMITclauses or implement basic caching.
Final Thoughts
The Model Context Protocol is still new, but it is the most robust way to stop treating AI like a isolated sandbox. By automating how Claude accesses your data, you eliminate the friction of manual context management. Start with a simple read-only tool for your most-used database. You will immediately notice how much faster you can ship code when the AI actually knows what your environment looks like.

