Update server.py
Browse files
server.py
CHANGED
@@ -1,9 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import uvicorn
|
|
|
3 |
|
4 |
from mcp.server.fastmcp import FastMCP
|
5 |
from starlette.requests import Request
|
6 |
-
from starlette.responses import PlainTextResponse,
|
7 |
|
8 |
from langchain_community.utilities import SQLDatabase
|
9 |
from langchain_community.tools.sql_database.tool import QuerySQLCheckerTool
|
@@ -16,37 +100,52 @@ llm = ChatOpenAI(
|
|
16 |
temperature=0
|
17 |
)
|
18 |
|
19 |
-
# Create an MCP server
|
20 |
mcp = FastMCP("Credit Card Database Server")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
credit_card_db = SQLDatabase.from_uri(r"sqlite:///data/ccms.db")
|
23 |
query_checker_tool = QuerySQLCheckerTool(db=credit_card_db, llm=llm)
|
24 |
|
25 |
-
|
26 |
@mcp.custom_route("/", methods=["GET"])
|
27 |
async def home(request: Request) -> PlainTextResponse:
|
28 |
return PlainTextResponse(
|
29 |
"""
|
30 |
Credit Card Database MCP Server
|
31 |
----
|
32 |
-
|
33 |
-
1. sql_db_list_tables: This tool can be used to list the tables in the database.
|
34 |
-
2. sql_db_schema: This tool can be used to get the schema of a table.
|
35 |
-
3. sql_db_query_checker: This tool can be used to check if a query is valid.
|
36 |
-
4. sql_db_query: This tool can be used to execute a query.
|
37 |
"""
|
38 |
)
|
39 |
|
40 |
-
|
41 |
-
@mcp.tool()
|
42 |
def sql_db_list_tables():
|
43 |
"""
|
44 |
Returns a comma-separated list of table names in the database.
|
45 |
"""
|
46 |
return credit_card_db.get_usable_table_names()
|
47 |
|
48 |
-
|
49 |
-
@mcp.tool()
|
50 |
def sql_db_schema(table_names: list[str]) -> str:
|
51 |
"""
|
52 |
Input 'table_names_str' is a comma-separated string of table names.
|
@@ -54,8 +153,7 @@ def sql_db_schema(table_names: list[str]) -> str:
|
|
54 |
"""
|
55 |
return credit_card_db.get_table_info(table_names)
|
56 |
|
57 |
-
|
58 |
-
@mcp.tool()
|
59 |
def sql_db_query_checker(query: str) -> str:
|
60 |
"""
|
61 |
Input 'query' is a SQL query string.
|
@@ -64,10 +162,9 @@ def sql_db_query_checker(query: str) -> str:
|
|
64 |
If the query is not valid, it returns the corrected query.
|
65 |
This tool is used to ensure the query is valid before executing it.
|
66 |
"""
|
67 |
-
|
68 |
return query_checker_tool.run(query)
|
69 |
|
70 |
-
@
|
71 |
def sql_db_query(query: str) -> str:
|
72 |
"""
|
73 |
Input 'query' is a SQL query string.
|
@@ -75,6 +172,10 @@ def sql_db_query(query: str) -> str:
|
|
75 |
"""
|
76 |
return credit_card_db.run(query)
|
77 |
|
|
|
|
|
|
|
|
|
78 |
|
79 |
if __name__ == "__main__":
|
80 |
uvicorn.run(mcp.streamable_http_app, host="0.0.0.0", port=8000)
|
|
|
1 |
+
# import os
|
2 |
+
# import uvicorn
|
3 |
+
|
4 |
+
# from mcp.server.fastmcp import FastMCP
|
5 |
+
# from starlette.requests import Request
|
6 |
+
# from starlette.responses import PlainTextResponse, Response
|
7 |
+
|
8 |
+
# from langchain_community.utilities import SQLDatabase
|
9 |
+
# from langchain_community.tools.sql_database.tool import QuerySQLCheckerTool
|
10 |
+
# from langchain_openai import ChatOpenAI
|
11 |
+
|
12 |
+
# llm = ChatOpenAI(
|
13 |
+
# api_key=os.environ.get('OPENAI_API_KEY', None),
|
14 |
+
# base_url=os.environ['OPENAI_BASE_URL'],
|
15 |
+
# model='gpt-4o-mini',
|
16 |
+
# temperature=0
|
17 |
+
# )
|
18 |
+
|
19 |
+
# # Create an MCP server
|
20 |
+
# mcp = FastMCP("Credit Card Database Server")
|
21 |
+
|
22 |
+
# credit_card_db = SQLDatabase.from_uri(r"sqlite:///data/ccms.db")
|
23 |
+
# query_checker_tool = QuerySQLCheckerTool(db=credit_card_db, llm=llm)
|
24 |
+
|
25 |
+
|
26 |
+
# @mcp.custom_route("/", methods=["GET"])
|
27 |
+
# async def home(request: Request) -> PlainTextResponse:
|
28 |
+
# return PlainTextResponse(
|
29 |
+
# """
|
30 |
+
# Credit Card Database MCP Server
|
31 |
+
# ----
|
32 |
+
# This server gives you access to the following tools:
|
33 |
+
# 1. sql_db_list_tables: This tool can be used to list the tables in the database.
|
34 |
+
# 2. sql_db_schema: This tool can be used to get the schema of a table.
|
35 |
+
# 3. sql_db_query_checker: This tool can be used to check if a query is valid.
|
36 |
+
# 4. sql_db_query: This tool can be used to execute a query.
|
37 |
+
# """
|
38 |
+
# )
|
39 |
+
|
40 |
+
|
41 |
+
# @mcp.tool()
|
42 |
+
# def sql_db_list_tables():
|
43 |
+
# """
|
44 |
+
# Returns a comma-separated list of table names in the database.
|
45 |
+
# """
|
46 |
+
# return credit_card_db.get_usable_table_names()
|
47 |
+
|
48 |
+
|
49 |
+
# @mcp.tool()
|
50 |
+
# def sql_db_schema(table_names: list[str]) -> str:
|
51 |
+
# """
|
52 |
+
# Input 'table_names_str' is a comma-separated string of table names.
|
53 |
+
# Returns the DDL SQL schema for these tables.
|
54 |
+
# """
|
55 |
+
# return credit_card_db.get_table_info(table_names)
|
56 |
+
|
57 |
+
|
58 |
+
# @mcp.tool()
|
59 |
+
# def sql_db_query_checker(query: str) -> str:
|
60 |
+
# """
|
61 |
+
# Input 'query' is a SQL query string.
|
62 |
+
# Checks if the query is valid.
|
63 |
+
# If the query is valid, it returns the original query.
|
64 |
+
# If the query is not valid, it returns the corrected query.
|
65 |
+
# This tool is used to ensure the query is valid before executing it.
|
66 |
+
# """
|
67 |
+
|
68 |
+
# return query_checker_tool.run(query)
|
69 |
+
|
70 |
+
# @mcp.tool()
|
71 |
+
# def sql_db_query(query: str) -> str:
|
72 |
+
# """
|
73 |
+
# Input 'query' is a SQL query string.
|
74 |
+
# Executes the query (SELECT only) and returns the result.
|
75 |
+
# """
|
76 |
+
# return credit_card_db.run(query)
|
77 |
+
|
78 |
+
|
79 |
+
# if __name__ == "__main__":
|
80 |
+
# uvicorn.run(mcp.streamable_http_app, host="0.0.0.0", port=8000)
|
81 |
+
|
82 |
+
|
83 |
+
|
84 |
import os
|
85 |
import uvicorn
|
86 |
+
import inspect
|
87 |
|
88 |
from mcp.server.fastmcp import FastMCP
|
89 |
from starlette.requests import Request
|
90 |
+
from starlette.responses import PlainTextResponse, JSONResponse
|
91 |
|
92 |
from langchain_community.utilities import SQLDatabase
|
93 |
from langchain_community.tools.sql_database.tool import QuerySQLCheckerTool
|
|
|
100 |
temperature=0
|
101 |
)
|
102 |
|
103 |
+
# Create an MCP server and the tool registry
|
104 |
mcp = FastMCP("Credit Card Database Server")
|
105 |
+
tool_registry = []
|
106 |
+
|
107 |
+
def register_tool(fn):
|
108 |
+
"""Decorator to register tool metadata and with MCP."""
|
109 |
+
# Register with MCP
|
110 |
+
mcp.tool()(fn)
|
111 |
+
# Save metadata
|
112 |
+
sig = inspect.signature(fn)
|
113 |
+
params = [
|
114 |
+
{
|
115 |
+
"name": param.name,
|
116 |
+
"type": str(param.annotation) if param.annotation is not inspect._empty else "Any",
|
117 |
+
"default": param.default if param.default is not inspect._empty else None,
|
118 |
+
}
|
119 |
+
for param in sig.parameters.values()
|
120 |
+
]
|
121 |
+
tool_registry.append({
|
122 |
+
"name": fn.__name__,
|
123 |
+
"description": fn.__doc__.strip() if fn.__doc__ else "",
|
124 |
+
"parameters": params,
|
125 |
+
})
|
126 |
+
return fn
|
127 |
|
128 |
credit_card_db = SQLDatabase.from_uri(r"sqlite:///data/ccms.db")
|
129 |
query_checker_tool = QuerySQLCheckerTool(db=credit_card_db, llm=llm)
|
130 |
|
|
|
131 |
@mcp.custom_route("/", methods=["GET"])
|
132 |
async def home(request: Request) -> PlainTextResponse:
|
133 |
return PlainTextResponse(
|
134 |
"""
|
135 |
Credit Card Database MCP Server
|
136 |
----
|
137 |
+
See /tools for a list of tools and their documentation.
|
|
|
|
|
|
|
|
|
138 |
"""
|
139 |
)
|
140 |
|
141 |
+
@register_tool
|
|
|
142 |
def sql_db_list_tables():
|
143 |
"""
|
144 |
Returns a comma-separated list of table names in the database.
|
145 |
"""
|
146 |
return credit_card_db.get_usable_table_names()
|
147 |
|
148 |
+
@register_tool
|
|
|
149 |
def sql_db_schema(table_names: list[str]) -> str:
|
150 |
"""
|
151 |
Input 'table_names_str' is a comma-separated string of table names.
|
|
|
153 |
"""
|
154 |
return credit_card_db.get_table_info(table_names)
|
155 |
|
156 |
+
@register_tool
|
|
|
157 |
def sql_db_query_checker(query: str) -> str:
|
158 |
"""
|
159 |
Input 'query' is a SQL query string.
|
|
|
162 |
If the query is not valid, it returns the corrected query.
|
163 |
This tool is used to ensure the query is valid before executing it.
|
164 |
"""
|
|
|
165 |
return query_checker_tool.run(query)
|
166 |
|
167 |
+
@register_tool
|
168 |
def sql_db_query(query: str) -> str:
|
169 |
"""
|
170 |
Input 'query' is a SQL query string.
|
|
|
172 |
"""
|
173 |
return credit_card_db.run(query)
|
174 |
|
175 |
+
@mcp.custom_route("/tools", methods=["GET"])
|
176 |
+
async def list_tools(request: Request) -> JSONResponse:
|
177 |
+
"""Return all registered tool metadata as JSON."""
|
178 |
+
return JSONResponse(tool_registry)
|
179 |
|
180 |
if __name__ == "__main__":
|
181 |
uvicorn.run(mcp.streamable_http_app, host="0.0.0.0", port=8000)
|