geqintan commited on
Commit
4469050
·
1 Parent(s): a823b8a
airs/base.py CHANGED
@@ -110,3 +110,9 @@ class Base:
110
  except Exception as e:
111
  print(f"退出登录出错: {str(e)}")
112
  return False
 
 
 
 
 
 
 
110
  except Exception as e:
111
  print(f"退出登录出错: {str(e)}")
112
  return False
113
+
114
+ def test(self):
115
+ """
116
+ 测试方法,返回一个简单的字符串
117
+ """
118
+ return "Base class test method called"
apps/airs_tasks.py CHANGED
@@ -3,6 +3,17 @@ from fastapi import Request
3
  from mcp.server.fastmcp import FastMCP
4
  import os # 用于获取环境变量
5
  from airs.base import Base # 导入 Base 类
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # 创建 MCP 应用实例
8
  class Tasks(Base): # Tasks 类继承自 Base 类
@@ -37,4 +48,37 @@ class Tasks(Base): # Tasks 类继承自 Base 类
37
  @self.mcp.tool()
38
  def app01_add_integers(num1: int, num2: int) -> int:
39
  """Add two integers in App01"""
 
40
  return num1 + num2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from mcp.server.fastmcp import FastMCP
4
  import os # 用于获取环境变量
5
  from airs.base import Base # 导入 Base 类
6
+ from pydantic import BaseModel, Field # 用于定义数据模型
7
+ from typing import Optional
8
+
9
+ # 定义 Pydantic 模型
10
+ class TaskCreate(BaseModel):
11
+ title: str = Field(..., description="任务的标题(必填)")
12
+ description: Optional[str] = Field(None, description="任务的详细描述(可选)") # 添加描述
13
+ due_date: Optional[str] = None # 可以是 'YYYY-MM-DD' 格式的字符串
14
+ status: Optional[str] = None
15
+ priority: Optional[str] = None
16
+ parent_task_id: Optional[str] = None # UUID 字符串,可选
17
 
18
  # 创建 MCP 应用实例
19
  class Tasks(Base): # Tasks 类继承自 Base 类
 
48
  @self.mcp.tool()
49
  def app01_add_integers(num1: int, num2: int) -> int:
50
  """Add two integers in App01"""
51
+ print(self.test()) # 调用 Base 类的 test 方法
52
  return num1 + num2
53
+
54
+ @self.mcp.tool()
55
+ def add_task(task: TaskCreate) -> dict:
56
+ """
57
+ Add a new task to the database.
58
+ 向数据库添加新任务
59
+
60
+ Args:
61
+ task (TaskCreate): 任务数据对象,包含以下属性:
62
+ - title (str): 任务标题
63
+ - description (str): 任务描述
64
+ - due_date (str): 截止日期(YYYY-MM-DD)
65
+ """
66
+ try:
67
+ # 直接使用 Supabase 客户端插入数据
68
+ data_to_insert = {
69
+ 'title': task.title,
70
+ 'description': task.description,
71
+ 'parent_task_id': task.parent_task_id,
72
+ 'due_date': task.due_date,
73
+ 'status': task.status,
74
+ 'priority': task.priority
75
+ }
76
+ response = self.supabase.table('tasks').insert(data_to_insert).execute()
77
+
78
+ # 检查 Supabase 响应
79
+ if response.data:
80
+ return {"success": True, "task": response.data[0]}
81
+ else:
82
+ return {"success": False, "error": response.error.message if response.error else "Unknown error"}
83
+ except Exception as e:
84
+ return {"success": False, "error": str(e)}
test/test_sb.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv # 导入 load_dotenv
2
+
3
+ # 加载 .env 文件中的环境变量
4
+ load_dotenv()
5
+
6
+ from supabase import create_client, Client
7
+ import os
8
+
9
+ # 从环境变量获取 Supabase URL 和 Key
10
+ # 在实际应用中,请确保这些变量已设置
11
+ supabase_url = os.getenv("SUPABASE_URL")
12
+ supabase_key = os.getenv("SUPABASE_KEY")
13
+
14
+
15
+ # !!! 请替换为你的 Supabase 项目 URL 和 anon key !!!
16
+ # SUPABASE_URL = "SUPABASE_URL"
17
+ # SUPABASE_KEY = "SUPABASE_KEY"
18
+
19
+ # 创建 Supabase 客户端
20
+ supabase: Client = create_client(supabase_url, supabase_key)
21
+
22
+ def test_insert_and_query_data():
23
+ """
24
+ 测试向 Supabase 插入数据并查询。
25
+ 请确保你的 Supabase 数据库中存在一个名为 'test_table' 的表,
26
+ 并且该表包含 'name' (text) 和 'age' (integer) 字段。
27
+ """
28
+ table_name = "test_table" # 替换为你的表名
29
+
30
+ print(f"正在向表 '{table_name}' 插入数据...")
31
+ # 插入数据
32
+ data_to_insert = {"name": "Test User", "age": 30}
33
+ response = supabase.table(table_name).insert(data_to_insert).execute()
34
+
35
+ if response.data:
36
+ print(f"数据插入成功: {response.data}")
37
+ else:
38
+ print(f"数据插入失败: {response.error}")
39
+ assert False, f"插入失败: {response.error}"
40
+
41
+ print(f"正在从表 '{table_name}' 查询数据...")
42
+ # 查询数据
43
+ response = supabase.table(table_name).select("*").eq("name", "Test User").execute()
44
+
45
+ if response.data:
46
+ print(f"数据查询成功: {response.data}")
47
+ assert len(response.data) > 0, "未查询到数据"
48
+ assert response.data[0]["name"] == "Test User"
49
+ else:
50
+ print(f"数据查询失败: {response.error}")
51
+ assert False, f"查询失败: {response.error}"
52
+
53
+ # 清理数据 (可选,根据测试需求决定是否保留)
54
+ print(f"正在从表 '{table_name}' 删除数据...")
55
+ response = supabase.table(table_name).delete().eq("name", "Test User").execute()
56
+ if response.data:
57
+ print(f"数据删除成功: {response.data}")
58
+ else:
59
+ print(f"数据删除失败: {response.error}")
60
+
61
+ if __name__ == "__main__":
62
+ print("开始运行 Supabase 测试...")
63
+ test_insert_and_query_data()
64
+ print("Supabase 测试完成。")
test/test_sb_create_table.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv # 导入 load_dotenv
2
+
3
+ # 加载 .env 文件中的环境变量
4
+ load_dotenv()
5
+
6
+ from supabase import create_client, Client
7
+ import os
8
+
9
+ # 从环境变量获取 Supabase URL 和 Key
10
+ # 在实际应用中,请确保这些变量已设置
11
+ supabase_url = os.getenv("SUPABASE_URL")
12
+ supabase_key = os.getenv("SUPABASE_KEY")
13
+
14
+
15
+ # !!! 请替换为你的 Supabase 项目 URL 和 anon key !!!
16
+ # SUPABASE_URL = "SUPABASE_URL"
17
+ # SUPABASE_KEY = "SUPABASE_KEY"
18
+
19
+ # 创建 Supabase 客户端
20
+ supabase: Client = create_client(supabase_url, supabase_key)
21
+
22
+ def create_test_table():
23
+ """
24
+ 创建 Supabase 中的 'test_table_1' 表。
25
+ 如果表已存在,则不会重复创建。
26
+ """
27
+ table_name = "test_table_1"
28
+ print(f"正在尝试通过 RPC 创建表 '{table_name}'...")
29
+
30
+ # Supabase Python SDK 不直接支持执行任意 DDL SQL。
31
+ # 最推荐的方式是在 Supabase 控制台手动创建表,或者通过迁移工具。
32
+ # 如果必须通过代码执行 DDL,通常需要:
33
+ # 1. 在 Supabase 数据库中创建一个 PostgreSQL 函数来封装 DDL 逻辑。
34
+ # 2. 然后通过 supabase.rpc() 调用这个函数。
35
+
36
+ # 以下是需要在 Supabase SQL 编辑器中执行的 PostgreSQL 函数定义:
37
+ print("\n--- 请在 Supabase SQL 编辑器中执行以下 SQL 来创建函数 'create_table_if_not_exists_func' ---")
38
+ print(f"""
39
+ CREATE OR REPLACE FUNCTION create_table_if_not_exists_func(table_name TEXT)
40
+ RETURNS VOID AS $$
41
+ BEGIN
42
+ EXECUTE format('
43
+ CREATE TABLE IF NOT EXISTS %I (
44
+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
45
+ name TEXT,
46
+ age INT
47
+ );
48
+ ', table_name);
49
+ END;
50
+ $$ LANGUAGE plpgsql;
51
+ """)
52
+ print("----------------------------------------------------------------------------------\n")
53
+
54
+ # 尝试通过 RPC 调用上述 PostgreSQL 函数
55
+ try:
56
+ response = supabase.rpc('create_table_if_not_exists_func', {'table_name': table_name}).execute()
57
+
58
+ if response.error:
59
+ print(f"通过 RPC 调用函数创建表失败: {response.error}")
60
+ # 如果错误是由于函数不存在,提示用户先创建函数
61
+ if "function create_table_if_not_exists_func(text) does not exist" in str(response.error):
62
+ print("错误提示:PostgreSQL 函数 'create_table_if_not_exists_func' 不存在。请先在 Supabase 控制台创建该函数。")
63
+ else:
64
+ print(f"通过 RPC 调用函数创建表成功: {response.data}")
65
+
66
+ except Exception as e:
67
+ print(f"调用 RPC 函数时发生意外错误: {e}")
68
+
69
+
70
+ def test_insert_and_query_data():
71
+ """
72
+ 测试向 Supabase 插入数据并查询。
73
+ 请确保你的 Supabase 数据库中存在一个名为 'test_table_1' 的表,
74
+ 并且该表包含 'name' (text) 和 'age' (integer) 字段。
75
+ """
76
+ table_name = "test_table_1" # 替换为你的表名
77
+
78
+ print(f"正在向表 '{table_name}' 插入数据...")
79
+ # 插入数据
80
+ data_to_insert = {"name": "Test User", "age": 30}
81
+ response = supabase.table(table_name).insert(data_to_insert).execute()
82
+
83
+ if response.data:
84
+ print(f"数据插入成功: {response.data}")
85
+ else:
86
+ print(f"数据插入失败: {response.error}")
87
+ assert False, f"插入失败: {response.error}"
88
+
89
+ print(f"正在从表 '{table_name}' 查询数据...")
90
+ # 查询数据
91
+ response = supabase.table(table_name).select("*").eq("name", "Test User").execute()
92
+
93
+ if response.data:
94
+ print(f"数据查询成功: {response.data}")
95
+ assert len(response.data) > 0, "未查询到数据"
96
+ assert response.data[0]["name"] == "Test User"
97
+ else:
98
+ print(f"数据查询失败: {response.error}")
99
+ assert False, f"查询失败: {response.error}"
100
+
101
+ # 清理数据 (可选,根据测试需求决定是否保留)
102
+ print(f"正在从表 '{table_name}' 删除数据...")
103
+ response = supabase.table(table_name).delete().eq("name", "Test User").execute()
104
+ if response.data:
105
+ print(f"数据删除成功: {response.data}")
106
+ else:
107
+ print(f"数据删除失败: {response.error}")
108
+
109
+ if __name__ == "__main__":
110
+ print("开始运行 Supabase 测试...")
111
+ create_test_table() # 在运行测试之前尝试创建表
112
+ test_insert_and_query_data() # 取消注释,以便在创建表后运行数据操作测试
113
+ print("Supabase 测试完成。")