Dev.to AI 🤖 Ai 👁 0 📖 4 min read

Creating MCP Server Using FastMCP in Python

The Model Context Protocol (MCP) is an emerging standard for structuring and managing contextual information provided to Large Language Models (LLMs), enabling them to operate more intelligently and consistently across s

The Model Context Protocol (MCP) is an emerging standard for structuring and managing contextual information provided to Large Language Models (LLMs), enabling them to operate more intelligently and consistently across sessions, tools, and multi-agent systems. Rather than relying solely on raw prompts, MCP introduces a structured format that includes user identity, task goals, memory state, available tools, and environmental data — allowing models to behave more like persistent, context-aware agents. This guide walks through running a sample MCP server built with FastMCP, backed by an SQLite database, and querying it using natural language through an MCP client. By the end, you'll have a working FastMCP server that an MCP client can query against a local SQLite database using plain language.

1. Get the Server Code Running

The code for the MCP server and the sample SQLite database is available in this GitHub repository. To set up the server locally, follow the steps below in your system terminal.

1. Clone the GitHub repository:

$ git clone https://github.com/vultr-marketing/code-samples.git

2. Navigate into the mcp_sqlite directory:

$ cd code-samples/mcp/mcp_sqlite

3. Create a .env file:

$ nano .env

4. Add the following variables to the file, then save and close it:

DB_PATH=sample.db
MCP_PORT=8080
READ_ONLY=true
  • DB_PATH: Path to your SQLite database file.
  • MCP_PORT: Port used by the MCP server (not used in stdio mode, but good to define).
  • READ_ONLY: Set to true to block insert/update/delete queries for safety.

5. Install dependencies:

$ pip3 install -r requirements.txt

2. Understand the Sample MCP Server

The server script launches a Model Context Protocol (MCP) server using FastMCP and connects it to a local SQLite database. It enables structured interaction with the database using natural language, typically via an LLM interface.

Key Features

  • Environment-Based Configuration: Uses .env for settings like DB_PATH, MCP_PORT, and READ_ONLY.
  • Async Database Access: Handles SQLite operations asynchronously with aiosqlite.
  • Sample DB Initialization: Creates sample tables if the database doesn't exist.
  • Query Validation: Prevents dangerous SQL operations, especially when READ_ONLY is enabled.
  • Structured Logging: Provides detailed startup and runtime logs for debugging.

Transport Layer: stdio

The server uses the stdio transport, allowing it to communicate over standard input and output. This is especially useful when integrating with desktop agents like Claude Desktop, enabling lightweight inter-process communication without a web server.

Available Tools in the Sample MCP Server

Tools are structured, callable functions that a language model can use to interact with external systems like databases, APIs, or services. Each tool has a defined input schema and a predictable output format, enabling the model to perform specific operations safely and effectively — like querying data, listing tables, or modifying records.

Tools extend the capabilities of LLMs beyond simple text generation, allowing them to reason over structured data, fetch real-time information, or take actions based on user intent.

Tool Name Description
execute_query Executes a validated SQL SELECT query and returns the result set.
list_tables Lists all user-defined tables in the SQLite database.
describe_table Returns the schema (columns, types, constraints) of a specified table.
count_rows Counts and returns the number of rows in a specified table.
insert_sample_data Inserts predefined sample data into tables (only if not read-only).

3. Use an MCP Client to Interact

Download the Claude Desktop application — it automatically detects MCP servers defined in its configuration file and makes them accessible for use within any conversation.

1. Start the Claude Desktop application.

2. Locate the settings option in the application.

3. Select Developer settings.

4. Click Edit Config.

5. Add the following configuration snippet to your Claude Desktop config file:

{
  "mcpServers": {
    "sqlite": {
      "command": "<path to uv executable>",
      "args": [
        "--directory",
        "<path to directory containing server.py>",
        "run",
        "server.py"
      ]
    }
  }
}

6. Once the new configuration is in place, restart the Claude Desktop application.

7. To confirm the server is running, go to the Developer settings in Claude Desktop and check if the status next to the JSON file shows as running.

8. Make sure the MCP server tools are available to the application.

Ask Questions and Perform Operations

You can run interactive queries by chatting naturally. Using the available tools, the LLM translates your requests into SQL queries, executes them on the sample database, and responds in plain language. The LLM also displays the database queries it is executing and the tools it is using to retrieve the data.

Note: the data in the sample database is for demonstration purposes only and may differ from actual data.

Next Steps

  • Point DB_PATH at a real SQLite database and extend the tool list beyond execute_query, list_tables, describe_table, and count_rows.
  • Swap the stdio transport for an HTTP-based transport if you need the server reachable from a remote MCP client.
  • Add authentication and request logging before exposing an MCP server with READ_ONLY=false beyond your local machine.
  • Try connecting the same server to other MCP-compatible clients, not just Claude Desktop.

For the full guide with additional tips, visit the original article on Vultr Docs.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.