BinaryONe commited on
Commit ·
931b5b0
1
Parent(s): a91f798
Initial Commit
Browse files- 0.13 +9 -0
- Dockerfile +25 -0
- requirements.txt +8 -0
- server.py +130 -0
0.13
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Collecting starlette
|
| 2 |
+
Using cached starlette-0.47.0-py3-none-any.whl.metadata (6.2 kB)
|
| 3 |
+
Requirement already satisfied: anyio<5,>=3.6.2 in c:\files\codes\research\mcp\.venv\lib\site-packages (from starlette) (4.9.0)
|
| 4 |
+
Requirement already satisfied: idna>=2.8 in c:\files\codes\research\mcp\.venv\lib\site-packages (from anyio<5,>=3.6.2->starlette) (3.10)
|
| 5 |
+
Requirement already satisfied: sniffio>=1.1 in c:\files\codes\research\mcp\.venv\lib\site-packages (from anyio<5,>=3.6.2->starlette) (1.3.1)
|
| 6 |
+
Requirement already satisfied: typing_extensions>=4.5 in c:\files\codes\research\mcp\.venv\lib\site-packages (from anyio<5,>=3.6.2->starlette) (4.14.0)
|
| 7 |
+
Using cached starlette-0.47.0-py3-none-any.whl (72 kB)
|
| 8 |
+
Installing collected packages: starlette
|
| 9 |
+
Successfully installed starlette-0.47.0
|
Dockerfile
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use an official Python 3.11 (or a future version if released) runtime as the base image
|
| 2 |
+
FROM python:3.11-slim
|
| 3 |
+
|
| 4 |
+
# Set environment variables to avoid Python writing .pyc files
|
| 5 |
+
# and to ensure that output is sent straight to the terminal
|
| 6 |
+
ENV PYTHONDONTWRITEBYTECODE 1
|
| 7 |
+
ENV PYTHONUNBUFFERED 1
|
| 8 |
+
|
| 9 |
+
# Set the working directory to /app
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
|
| 12 |
+
# Copy the requirements file into the container
|
| 13 |
+
COPY requirements.txt /app/
|
| 14 |
+
|
| 15 |
+
# Install the dependencies
|
| 16 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 17 |
+
|
| 18 |
+
# Copy the entire project into the container
|
| 19 |
+
COPY . /app/
|
| 20 |
+
|
| 21 |
+
# Expose the server port (7860)
|
| 22 |
+
EXPOSE 7860
|
| 23 |
+
|
| 24 |
+
# Run the FastMCP server using uvicorn (with FastAPI)
|
| 25 |
+
CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "7860"]
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
fastapi-mcp>=0.2.0,
|
| 3 |
+
ipykernel>=6.29.5,
|
| 4 |
+
linkup-sdk>=0.2.4,
|
| 5 |
+
llama-index>=0.12.30,
|
| 6 |
+
llama-index-llms-ollama>=0.5.4,
|
| 7 |
+
llama-index-tools-mcp>=0.1.1,
|
| 8 |
+
mcp[cli]>=1.6.0
|
server.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
import argparse
|
| 3 |
+
from mcp.server.fastmcp import FastMCP
|
| 4 |
+
|
| 5 |
+
mcp = FastMCP('sqlite-demo')
|
| 6 |
+
|
| 7 |
+
def init_db():
|
| 8 |
+
conn = sqlite3.connect('demo.db')
|
| 9 |
+
cursor = conn.cursor()
|
| 10 |
+
cursor.execute('''
|
| 11 |
+
CREATE TABLE IF NOT EXISTS people (
|
| 12 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 13 |
+
name TEXT NOT NULL,
|
| 14 |
+
age INTEGER NOT NULL,
|
| 15 |
+
profession TEXT NOT NULL
|
| 16 |
+
)
|
| 17 |
+
''')
|
| 18 |
+
conn.commit()
|
| 19 |
+
return conn, cursor
|
| 20 |
+
|
| 21 |
+
@mcp.tool()
|
| 22 |
+
def add_data(query: str) -> bool:
|
| 23 |
+
"""Add new data to the people table using a SQL INSERT query.
|
| 24 |
+
|
| 25 |
+
Args:
|
| 26 |
+
query (str): SQL INSERT query following this format:
|
| 27 |
+
INSERT INTO people (name, age, profession)
|
| 28 |
+
VALUES ('John Doe', 30, 'Engineer')
|
| 29 |
+
|
| 30 |
+
Schema:
|
| 31 |
+
- name: Text field (required)
|
| 32 |
+
- age: Integer field (required)
|
| 33 |
+
- profession: Text field (required)
|
| 34 |
+
Note: 'id' field is auto-generated
|
| 35 |
+
|
| 36 |
+
Returns:
|
| 37 |
+
bool: True if data was added successfully, False otherwise
|
| 38 |
+
|
| 39 |
+
Example:
|
| 40 |
+
>>> query = '''
|
| 41 |
+
... INSERT INTO people (name, age, profession)
|
| 42 |
+
... VALUES ('Alice Smith', 25, 'Developer')
|
| 43 |
+
... '''
|
| 44 |
+
>>> add_data(query)
|
| 45 |
+
True
|
| 46 |
+
"""
|
| 47 |
+
conn, cursor = init_db()
|
| 48 |
+
try:
|
| 49 |
+
cursor.execute(query)
|
| 50 |
+
conn.commit()
|
| 51 |
+
return True
|
| 52 |
+
except sqlite3.Error as e:
|
| 53 |
+
print(f"Error adding data: {e}")
|
| 54 |
+
return False
|
| 55 |
+
finally:
|
| 56 |
+
conn.close()
|
| 57 |
+
|
| 58 |
+
@mcp.tool()
|
| 59 |
+
def read_data(query: str = "SELECT * FROM people") -> list:
|
| 60 |
+
"""Read data from the people table using a SQL SELECT query.
|
| 61 |
+
|
| 62 |
+
Args:
|
| 63 |
+
query (str, optional): SQL SELECT query. Defaults to "SELECT * FROM people".
|
| 64 |
+
Examples:
|
| 65 |
+
- "SELECT * FROM people"
|
| 66 |
+
- "SELECT name, age FROM people WHERE age > 25"
|
| 67 |
+
- "SELECT * FROM people ORDER BY age DESC"
|
| 68 |
+
|
| 69 |
+
Returns:
|
| 70 |
+
list: List of tuples containing the query results.
|
| 71 |
+
For default query, tuple format is (id, name, age, profession)
|
| 72 |
+
|
| 73 |
+
Example:
|
| 74 |
+
>>> # Read all records
|
| 75 |
+
>>> read_data()
|
| 76 |
+
[(1, 'John Doe', 30, 'Engineer'), (2, 'Alice Smith', 25, 'Developer')]
|
| 77 |
+
|
| 78 |
+
>>> # Read with custom query
|
| 79 |
+
>>> read_data("SELECT name, profession FROM people WHERE age < 30")
|
| 80 |
+
[('Alice Smith', 'Developer')]
|
| 81 |
+
"""
|
| 82 |
+
conn, cursor = init_db()
|
| 83 |
+
try:
|
| 84 |
+
cursor.execute(query)
|
| 85 |
+
return cursor.fetchall()
|
| 86 |
+
except sqlite3.Error as e:
|
| 87 |
+
print(f"Error reading data: {e}")
|
| 88 |
+
return []
|
| 89 |
+
finally:
|
| 90 |
+
conn.close()
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
# Start the server
|
| 96 |
+
print("🚀Starting server... ")
|
| 97 |
+
|
| 98 |
+
# Debug Mode
|
| 99 |
+
# uv run mcp dev server.py
|
| 100 |
+
|
| 101 |
+
# Production Mode
|
| 102 |
+
# uv run server.py --server_type=sse
|
| 103 |
+
|
| 104 |
+
parser = argparse.ArgumentParser()
|
| 105 |
+
parser.add_argument(
|
| 106 |
+
"--server_type", type=str, default="sse", choices=["sse", "stdio"]
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
args = parser.parse_args()
|
| 110 |
+
mcp.run(args.server_type)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
# # Example usage
|
| 115 |
+
# if __name__ == "__main__":
|
| 116 |
+
# # Example INSERT query
|
| 117 |
+
# insert_query = """
|
| 118 |
+
# INSERT INTO people (name, age, profession)
|
| 119 |
+
# VALUES ('John Doe', 30, 'Engineer')
|
| 120 |
+
# """
|
| 121 |
+
|
| 122 |
+
# # Add data
|
| 123 |
+
# if add_data(insert_query):
|
| 124 |
+
# print("Data added successfully")
|
| 125 |
+
|
| 126 |
+
# # Read all data
|
| 127 |
+
# results = read_data()
|
| 128 |
+
# print("\nAll records:")
|
| 129 |
+
# for record in results:
|
| 130 |
+
# print(record)
|