space-sue commited on
Commit
1037df5
·
1 Parent(s): 20b7c40

standalone mcp server script

Browse files
Files changed (1) hide show
  1. mcp_server_standalone.py +70 -0
mcp_server_standalone.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Real MCP Server for Fire Detection
2
+ # Copyright (c) 2024 Wild Fire Tracker
3
+ # Licensed under MIT License - see LICENSE file for details
4
+
5
+ import asyncio
6
+ import json
7
+ import base64
8
+ from mcp.server import Server
9
+ from mcp.server.stdio import stdio_server
10
+ from mcp.types import Tool, TextContent
11
+ from app import FireDetectionMCP
12
+ import cv2
13
+ import numpy as np
14
+
15
+ # Initialize fire detection
16
+ fire_detector = FireDetectionMCP()
17
+
18
+ # Create MCP server
19
+ server = Server("fire-detection-server")
20
+
21
+ @server.list_tools()
22
+ async def list_tools():
23
+ return [
24
+ Tool(
25
+ name="analyze_image",
26
+ description="Analyze image for fire and smoke detection",
27
+ inputSchema={
28
+ "type": "object",
29
+ "properties": {
30
+ "image_base64": {
31
+ "type": "string",
32
+ "description": "Base64 encoded image"
33
+ }
34
+ },
35
+ "required": ["image_base64"]
36
+ }
37
+ )
38
+ ]
39
+
40
+ @server.call_tool()
41
+ async def call_tool(name: str, arguments: dict):
42
+ if name == "analyze_image":
43
+ try:
44
+ # Decode base64 image
45
+ image_data = base64.b64decode(arguments["image_base64"])
46
+ nparr = np.frombuffer(image_data, np.uint8)
47
+ frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
48
+
49
+ # Analyze frame
50
+ status_emoji, status_plain, color = fire_detector.analyze_frame(frame)
51
+
52
+ return [TextContent(
53
+ type="text",
54
+ text=json.dumps({
55
+ "status": status_emoji,
56
+ "color": color,
57
+ "timestamp": fire_detector.last_detection_time
58
+ })
59
+ )]
60
+ except Exception as e:
61
+ return [TextContent(type="text", text=f"Error: {str(e)}")]
62
+
63
+ raise ValueError(f"Unknown tool: {name}")
64
+
65
+ async def main():
66
+ async with stdio_server() as streams:
67
+ await server.run(*streams)
68
+
69
+ if __name__ == "__main__":
70
+ asyncio.run(main())