Aulafy
Courses/Agents and automation/Create a Custom MCP Server

Create a Custom MCP Server

Installing public MCP servers is fine for learning. But the useful leap comes when you expose your own tools: search your documentation, validate a repo, query an internal database, or generate a focused report.

  • Design a minimal MCP server with narrow tools.
  • Separate business logic, validation, and MCP transport.
  • Test it before connecting it to Claude Code, Hermes, or another client.
TerminalCode
# Estructura mínima
mcp-debug/
  server.py
  pyproject.toml
  tests/
    test_tools.py

# Tool útil:
# run_lint(project_path) -> ejecuta solo comandos permitidos
# search_docs(query) -> busca en una carpeta concreta
TerminalCode
from mcp.server.fastmcp import FastMCP
from pathlib import Path
import subprocess

mcp = FastMCP("aulafy-debug-tools")
ROOT = Path("/Users/me/proyectos").resolve()

def safe_path(path: str) -> Path:
    target = Path(path).resolve()
    if ROOT not in target.parents and target != ROOT:
        raise ValueError("Path fuera del workspace permitido")
    return target

@mcp.tool()
def list_markdown_files(path: str) -> list[str]:
    base = safe_path(path)
    return [str(p.relative_to(base)) for p in base.rglob("*.md")][:100]

@mcp.tool()
def run_lint(path: str) -> str:
    base = safe_path(path)
    result = subprocess.run(
        ["npm", "run", "lint"],
        cwd=base,
        text=True,
        capture_output=True,
        timeout=120,
    )
    return result.stdout[-6000:] + result.stderr[-3000:]

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

Checklist Before Connecting an Agent

  • The tool rejects paths outside the workspace.
  • There is a timeout and output limit.
  • It does not accept arbitrary commands.
  • It logs arguments, decision, and result.
  • It has tests with malicious inputs.

Official Sources

  • Model Context Protocol introduction
  • MCP: build a server
  • MCP Python SDK
  • MCP Python SDK GitHub