Files
project_test/SmartVoyage/mcp_server/practice_mcp.py
User 5033b5f97e Initial commit
- Add mymath.py with multiply and divide functions
- Add SmartVoyage travel assistant project
- Add .gitignore for Python cache files

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-02 15:05:06 +08:00

35 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import mysql.connector
import json
from datetime import datetime,date,timedelta
from decimal import Decimal
from mcp.server.fastmcp import FastMCP
from SmartVoyage.config import Config
from SmartVoyage.create_logger import logger
from SmartVoyage.utils.format import DateEncoder,default_encoder
config=Config()
#票务服务
class TicketService:#定义票务服务类,封装数据库操作逻辑
def __init__(self):#初始化方法,建立数据库连接
#连接数据库
self.conn=mysql.connector.connect(
host=config.host,
user=config.user,
password=config.password,
database=config.database
)
#定义执行sql查询方法输入SQL字符串返回JSON字符串
def execute_query(self,sql:str) -> str:
try:
cursor=self.conn.cursor(dictionary=True)
cursor.execute(sql)
resultes=cursor.fetchall()
cursor.close()
#格式化结果
for resulte in resultes:#遍历每个结果字典
for key,value in resulte.items():
if isinstance(value,(date,datetime,timedelta,Decimal)):#检查值是否是特殊类型
resulte[key]=default_encoder(value)#使用自定义编码器格式化该值