aeonshift commited on
Commit
2245de1
·
verified ·
1 Parent(s): 79c7bb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -1
app.py CHANGED
@@ -1,9 +1,97 @@
 
 
 
1
  from fastapi import FastAPI
 
 
 
2
  import uvicorn
3
- import os
4
 
5
  app = FastAPI()
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  @app.get("/health")
8
  async def health_check():
9
  return {"status": "healthy"}
 
1
+ import os
2
+ import json
3
+ import requests
4
  from fastapi import FastAPI
5
+ from mcp.server.lowlevel import Server, NotificationOptions
6
+ from mcp.server.sse import SSEServerTransport
7
+ from mcp import types as mcp_types
8
  import uvicorn
 
9
 
10
  app = FastAPI()
11
 
12
+ # Load environment variables
13
+ AIRTABLE_API_TOKEN = os.getenv("AIRTABLE_API_TOKEN")
14
+ AIRTABLE_BASE_ID = os.getenv("AIRTABLE_BASE_ID")
15
+ TABLE_ID = "tblQECi5f7m4y2NEV"
16
+ AIRTABLE_API_URL = f"https://api.airtable.com/v0/{AIRTABLE_BASE_ID}/{TABLE_ID}"
17
+
18
+ # Helper function for Airtable API requests
19
+ def airtable_request(method, endpoint="", data=None):
20
+ headers = {
21
+ "Authorization": f"Bearer {AIRTABLE_API_TOKEN}",
22
+ "Content-Type": "application/json"
23
+ }
24
+ url = f"{AIRTABLE_API_URL}/{endpoint}" if endpoint else AIRTABLE_API_URL
25
+ response = requests.request(method, url, headers=headers, json=data)
26
+ response.raise_for_status()
27
+ return response.json()
28
+
29
+ # Tool to list records
30
+ async def list_records_tool(request: mcp_types.CallToolRequest):
31
+ try:
32
+ records = airtable_request("GET")
33
+ return {
34
+ "success": True,
35
+ "result": json.dumps(records)
36
+ }
37
+ except Exception as e:
38
+ return {
39
+ "success": False,
40
+ "error": str(e)
41
+ }
42
+
43
+ # Tool to create a record
44
+ async def create_record_tool(request: mcp_types.CallToolRequest):
45
+ try:
46
+ record_data = request.input.get("record_data", {})
47
+ data = {"records": [{"fields": record_data}]}
48
+ response = airtable_request("POST", data=data)
49
+ return {
50
+ "success": True,
51
+ "result": json.dumps(response)
52
+ }
53
+ except Exception as e:
54
+ return {
55
+ "success": False,
56
+ "error": str(e)
57
+ }
58
+
59
+ # MCP Server setup
60
+ async def start_mcp_server():
61
+ tools = [
62
+ mcp_types.ToolDefinition(
63
+ name="list_airtable_records",
64
+ description="Lists all records in the specified Airtable table",
65
+ input_schema={},
66
+ output_schema={"type": "string"}
67
+ ),
68
+ mcp_types.ToolDefinition(
69
+ name="create_airtable_record",
70
+ description="Creates a new record in the specified Airtable table",
71
+ input_schema={"record_data": {"type": "object"}},
72
+ output_schema={"type": "string"}
73
+ )
74
+ ]
75
+
76
+ server = Server(
77
+ tools=tools,
78
+ tool_handlers={
79
+ "list_airtable_records": list_records_tool,
80
+ "create_airtable_record": create_record_tool
81
+ },
82
+ notification_options=NotificationOptions()
83
+ )
84
+
85
+ transport = SSEServerTransport(path="/airtable/mcp")
86
+ await server.run(transport)
87
+
88
+ # Start the MCP server when the FastAPI app starts
89
+ @app.on_event("startup")
90
+ async def startup_event():
91
+ import asyncio
92
+ asyncio.create_task(start_mcp_server())
93
+
94
+ # Health check endpoint
95
  @app.get("/health")
96
  async def health_check():
97
  return {"status": "healthy"}