Skip to main content

Natural Language Model Database - Query databases using natural language

Project description

Model Context Protocol (MCP) Library

A Python library for connecting to and querying databases using the OpenAI API and LangChain agents. This library implements the Model Context Protocol (MCP) approach, providing AI models with structured context about database schemas for more accurate database-related responses.

Features

  • Connect to SQLite databases (extensible to other database types)
  • Generate structured database context for LLMs
  • Create LangChain agents for database interaction
  • Execute SQL queries and get results
  • Direct querying of LLMs with database context

Installation

pip install mcp-lib

Quick Start

from nlmdb import Config, DatabaseHandler, DatabaseTools, MCPHandler, create_database_agent
from openai import OpenAI

# Set up configuration
config = Config(openai_api_key="your-api-key", db_path="your-database.db")

# Initialize database handler
db_handler = DatabaseHandler(config.db_path)

# Initialize database tools
db_tools = DatabaseTools(db_handler)
tools = db_tools.get_tools()

# Initialize OpenAI client
client = OpenAI(api_key=config.openai_api_key)

# Initialize MCP handler
mcp_handler = MCPHandler(client, db_handler)

# Create agent executor
agent_executor = create_database_agent(
    openai_api_key=config.openai_api_key,
    tools=tools,
    mcp_handler=mcp_handler
)

# Run a query
response = agent_executor.invoke({"input": "What tables are in the database?"})
print(response["output"])

Library Structure

mcp_lib/
├── __init__.py
├── config.py
├── database/
│   ├── __init__.py
│   ├── handler.py
│   └── tools.py
├── mcp/
│   ├── __init__.py
│   └── handler.py
├── agents/
│   ├── __init__.py
│   └── agent_factory.py
└── examples/
    ├── __init__.py
    └── example_queries.py

Examples

The library includes several example functions demonstrating its usage:

from mcp_lib.examples import run_examples

# Run all examples
run_examples(api_key="your-api-key", db_path="your-database.db")

Requirements

  • Python 3.8+
  • openai>=1.0.0
  • langchain>=0.1.0
  • langchain-core>=0.1.0
  • langchain-experimental>=0.0.0
  • langchain-community>=0.0.0

License

MIT

Acknowledgements

This library is based on the Model Context Protocol (MCP) approach for providing AI models with structured context about database schemas.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nlmdb-1.3.1.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nlmdb-1.3.1-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file nlmdb-1.3.1.tar.gz.

File metadata

  • Download URL: nlmdb-1.3.1.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for nlmdb-1.3.1.tar.gz
Algorithm Hash digest
SHA256 93dcda1da2ec79289f452bea75aeaaf336b6f65c76c774384b9db5da974c34de
MD5 fe81622328fb8d2ddb5d7d8080bb660a
BLAKE2b-256 0e1a3d9b42943d5e4e1976335b53b876615ca0a2a5865817443e79774cb6f4c3

See more details on using hashes here.

File details

Details for the file nlmdb-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: nlmdb-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for nlmdb-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 11485da6f286f2c9418cfb50e7e0530c3ec82834d4a5bc752226aaa1c3b25a08
MD5 e12971ff65775dac16be2cc5ad73c1e0
BLAKE2b-256 2fbcaff483a116b761de3f079fc6e5fcbb7e1fb362f0707d2080c9f83f208c15

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page