Skip to content

omkarb31/mcp-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

MCP Database Server

A Model Context Protocol (MCP) server for managing a local SQLite database using FastMCP with a streamable HTTP transport, plus an LLM-powered MCP client that can reason about user questions and automatically decide which database tools to call.


🌟 Overview

This project demonstrates:

  • βœ… An MCP-compliant database server
  • βœ… SQLite-backed CRUD operations exposed as MCP tools
  • βœ… HTTP (streamable) MCP transport
  • βœ… A Python client using LangChain + Ollama
  • βœ… Real LLM-driven decision-making over MCP tools

πŸš€ Quick Start

1. Install Dependencies

pip install -r requirements.txt

2. Run the MCP Server

python main_app.py

3. Server Endpoints

⚠️ MCP must be mounted at the root path using mcp.streamable_http_app().


✨ Features

MCP Server

  • πŸ”Œ Model Context Protocol (MCP) compliant
  • ⚑ FastMCP with streamable HTTP transport
  • πŸ’Ύ SQLite local database
  • πŸ› οΈ 8 structured database tools
  • πŸ“Š JSON-formatted responses
  • 🏠 Simple local deployment

MCP Client / LLM Agent

  • 🌐 Streamable HTTP MCP client
  • πŸ” Automatic tool discovery
  • πŸ”— LangChain StructuredTool integration
  • πŸ€– Ollama-powered local LLM
  • 🧠 Multi-step tool execution and reasoning loop

βš™οΈ Environment Variables

DB_FILE=data.db
HOST=127.0.0.1
PORT=8000

Example (PowerShell)

$env:DB_FILE="C:\path\to\data.db"
$env:PORT="8000"
python server.py

πŸ—„οΈ Database Schema

Users Table

CREATE TABLE users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    email TEXT NOT NULL UNIQUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Products Table

CREATE TABLE products (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    price REAL NOT NULL,
    stock INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

πŸ› οΈ Available MCP Tools

Tool Name Description
execute_query Execute SQL SELECT queries
insert_user Insert a new user
insert_product Insert a new product
update_user Update user name or email
delete_user Delete a user by ID
get_all_users Retrieve all users
get_all_products Retrieve all products
get_database_info View database schema information

All tools return formatted JSON strings.


πŸ€– LLM-Powered MCP Client

The client (client.py) connects to the MCP server and allows an LLM to:

  1. πŸ” Discover available MCP tools
  2. πŸ’¬ Analyze natural-language questions
  3. 🎯 Decide which tools to call
  4. βš™οΈ Execute tools automatically
  5. βœ… Produce a final response

Requirements (Client)

Install Ollama:

ollama pull llama3.2:1b
# or for better tool calling:
ollama pull llama3.2:3b

Install Python dependencies:

pip install langchain-ollama langchain-core mcp

Running the Client

Start the MCP server first:

python main_app.py

Then run the client:

python client.py

Example Queries

The LLM dynamically selects and executes the correct MCP tools:

βœ… "Show me all users in the database"
βœ… "Add a new user named Charlie Brown with email charlie@peanuts.com"
βœ… "What's the structure of the database?"
βœ… "List all products available"
βœ… "Insert a product called Laptop with price 999.99"
image ---

πŸ“ Project Structure

.
β”œβ”€β”€ main_app.py            # MCP server (FastMCP)
β”œβ”€β”€ database_tools.py    # Database CRUD operations
β”œβ”€β”€ client.py            # LLM-powered MCP client
β”œβ”€β”€ requirements.txt     # Python dependencies
β”œβ”€β”€ data.db              # SQLite database (auto-created)
└── README.md            # This file

πŸ§ͺ Testing

Test the MCP server with:

  1. Claude Desktop (MCP integration)
  2. Custom Python client (client.py)
  3. Any MCP-compatible client

Manual Tool Testing

python test_fastmcp.py

πŸ”’ Security Notes

⚠️ DNS rebinding protection is disabled by default for local development:

TransportSecuritySettings(enable_dns_rebinding_protection=False)

Enable this for production deployments.


πŸ“¦ requirements.txt

fastapi
uvicorn[standard]
mcp
langchain-ollama
langchain-core

🎯 How It Works

Server Flow

User Query β†’ MCP Client β†’ HTTP Request β†’ FastMCP Server β†’ Database Tools β†’ SQLite
                                                                          ↓
User Answer ← LLM Processing ← Tool Results ← JSON Response ← Database Query

Client Flow

1. Connect to MCP server via streamable HTTP
2. Discover available tools
3. Convert MCP tools to LangChain tools
4. Bind tools to LLM (Ollama)
5. User asks question in natural language
6. LLM analyzes question and decides which tools to call
7. Execute tools via MCP
8. LLM formulates final answer

🀝 Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

πŸ™ Acknowledgments


πŸ“ž Support

If you encounter issues:

  1. Check that Ollama is running: ollama list
  2. Verify server is running: curl http://127.0.0.1:8000/health
  3. Check server logs for errors
  4. Try a larger model: ollama pull llama3.2:3b

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages