Sub topic 3
Writing Your Own Minimal MCP Server
Assume basic Python, JSON, command-line use, and familiarity with an AI coding assistant. An MCP server exposes capabilities through the Model Context Protocol. A tool is a callable function with a name, input schema, and result.
Build a small server
The official Python SDK can create a server from a decorated function:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("calculator")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two integers."""
return a + b
if __name__ == "__main__":
mcp.run() # Uses stdio by defaultThe function name becomes the tool name. Type hints describe its inputs, and the docstring describes its purpose. Install the SDK with pip install mcp, then save this code as server.py.
A coding assistant can launch it with a configuration such as:
{
"mcpServers": {
"calculator": {
"command": "python",
"args": ["server.py"]
}
}
}Debug with the MCP Inspector
The MCP Inspector starts a server independently of the assistant:
npx @modelcontextprotocol/inspector python server.pyOpen the displayed local address. List the server's tools, inspect the generated schema, and call add with sample values. This separates server errors from assistant behavior. For deeper debugging, inspect initialization, tools/list, and tools/call messages in the Inspector or server logs.
Security and trust
An MCP server runs code with the permissions of its process. Review its source, dependencies, requested files, network access, and environment variables before adding it. Pass secrets through environment variables or a secret manager, not source code. Treat tool results and retrieved content as untrusted input because they can contain prompt-injection instructions.
Remote servers and authorization
Remote servers use HTTP and may require authentication, such as an OAuth flow or an access token. Verify the server's URL and identity, use HTTPS, grant only needed scopes, and avoid sending private repository data unless required.
Choose servers carefully
Each server adds tools, descriptions, permissions, and context for the assistant to consider. Prefer small, focused servers with clear schemas. Disable unused servers, expose narrow read-only tools where possible, and separate high-risk write actions from inspection tools.
Exercise
Why should a server's diagnostic messages go to standard error when it uses stdio?
Show answer
Standard output carries protocol messages. Diagnostic text there can corrupt the MCP message stream.