File size: 9,645 Bytes
5369733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""
MCP Server for CleanCity Agent

Exposes trash detection and cleanup planning tools via the Model Context Protocol.
Can be used by Claude Desktop or other MCP clients.
"""

from mcp.server import Server
from mcp.types import Tool, TextContent
import mcp.server.stdio
import json
from typing import Any

from tools import (
    detect_trash_mcp,
    plan_cleanup,
    log_event,
    query_events,
    generate_report
)
from tools.history_tool import get_hotspots, mark_cleaned


# Initialize MCP server
server = Server("cleancity-agent")


@server.list_tools()
async def list_tools() -> list[Tool]:
    """List available MCP tools."""
    return [
        Tool(
            name="detect_trash",
            description="Detect trash objects in an image using computer vision. Returns bounding boxes, labels, and confidence scores.",
            inputSchema={
                "type": "object",
                "properties": {
                    "image_data": {
                        "type": "string",
                        "description": "Base64 encoded image data"
                    }
                },
                "required": ["image_data"]
            }
        ),
        Tool(
            name="plan_cleanup",
            description="Generate a cleanup action plan based on detected trash. Returns severity level, resource requirements, and recommendations.",
            inputSchema={
                "type": "object",
                "properties": {
                    "detections": {
                        "type": "array",
                        "description": "Array of trash detection objects from detect_trash",
                        "items": {"type": "object"}
                    },
                    "location": {
                        "type": "string",
                        "description": "Location description (optional)"
                    },
                    "notes": {
                        "type": "string",
                        "description": "Additional context or notes (optional)"
                    },
                    "use_llm": {
                        "type": "boolean",
                        "description": "Use LLM for enhanced planning (optional, default: false)"
                    }
                },
                "required": ["detections"]
            }
        ),
        Tool(
            name="log_event",
            description="Log a trash detection event to the history database for tracking and analysis.",
            inputSchema={
                "type": "object",
                "properties": {
                    "detections": {
                        "type": "array",
                        "description": "Array of trash detection objects",
                        "items": {"type": "object"}
                    },
                    "severity": {
                        "type": "string",
                        "description": "Severity level: low, medium, or high"
                    },
                    "location": {
                        "type": "string",
                        "description": "Location description (optional)"
                    },
                    "notes": {
                        "type": "string",
                        "description": "User notes (optional)"
                    }
                },
                "required": ["detections", "severity"]
            }
        ),
        Tool(
            name="query_events",
            description="Query trash events from history database with filtering options. Useful for finding patterns and hotspots.",
            inputSchema={
                "type": "object",
                "properties": {
                    "days": {
                        "type": "integer",
                        "description": "Only events from last N days (optional)"
                    },
                    "location": {
                        "type": "string",
                        "description": "Filter by location (partial match, optional)"
                    },
                    "severity": {
                        "type": "string",
                        "description": "Filter by severity: low, medium, high (optional)"
                    },
                    "limit": {
                        "type": "integer",
                        "description": "Maximum results to return (default: 100)"
                    }
                }
            }
        ),
        Tool(
            name="get_hotspots",
            description="Identify locations with recurring trash problems based on historical data.",
            inputSchema={
                "type": "object",
                "properties": {
                    "min_events": {
                        "type": "integer",
                        "description": "Minimum events to qualify as hotspot (default: 2)"
                    },
                    "days": {
                        "type": "integer",
                        "description": "Time window in days (optional, default: 30)"
                    }
                }
            }
        ),
        Tool(
            name="generate_report",
            description="Generate a formatted report for trash detection event, suitable for city authorities or documentation.",
            inputSchema={
                "type": "object",
                "properties": {
                    "detections": {
                        "type": "array",
                        "description": "Array of trash detection objects",
                        "items": {"type": "object"}
                    },
                    "severity": {
                        "type": "string",
                        "description": "Severity level"
                    },
                    "location": {
                        "type": "string",
                        "description": "Location description (optional)"
                    },
                    "notes": {
                        "type": "string",
                        "description": "Additional notes (optional)"
                    },
                    "plan": {
                        "type": "object",
                        "description": "Cleanup plan object (optional)"
                    },
                    "format": {
                        "type": "string",
                        "description": "Report format: email, markdown, or plain (default: email)"
                    }
                },
                "required": ["detections", "severity"]
            }
        ),
        Tool(
            name="mark_cleaned",
            description="Mark a logged event as cleaned/resolved.",
            inputSchema={
                "type": "object",
                "properties": {
                    "event_id": {
                        "type": "integer",
                        "description": "Database event ID to mark as cleaned"
                    }
                },
                "required": ["event_id"]
            }
        )
    ]


@server.call_tool()
async def call_tool(name: str, arguments) -> list[TextContent]:
    """Handle tool execution."""
    try:
        if name == "detect_trash":
            result = detect_trash_mcp(arguments["image_data"])
        
        elif name == "plan_cleanup":
            result = plan_cleanup(
                detections=arguments["detections"],
                location=arguments.get("location"),
                notes=arguments.get("notes"),
                use_llm=arguments.get("use_llm", False)
            )
        
        elif name == "log_event":
            result = log_event(
                detections=arguments["detections"],
                severity=arguments["severity"],
                location=arguments.get("location"),
                notes=arguments.get("notes")
            )
        
        elif name == "query_events":
            result = query_events(
                days=arguments.get("days"),
                location=arguments.get("location"),
                severity=arguments.get("severity"),
                limit=arguments.get("limit", 100)
            )
        
        elif name == "get_hotspots":
            result = get_hotspots(
                min_events=arguments.get("min_events", 2),
                days=arguments.get("days", 30)
            )
        
        elif name == "generate_report":
            result = generate_report(
                detections=arguments["detections"],
                severity=arguments["severity"],
                location=arguments.get("location"),
                notes=arguments.get("notes"),
                plan=arguments.get("plan"),
                format=arguments.get("format", "email")
            )
        
        elif name == "mark_cleaned":
            result = mark_cleaned(arguments["event_id"])
        
        else:
            return [TextContent(
                type="text",
                text=f"Unknown tool: {name}"
            )]
        
        # Return result as JSON
        return [TextContent(
            type="text",
            text=json.dumps(result, indent=2)
        )]
    
    except Exception as e:
        return [TextContent(
            type="text",
            text=f"Error executing {name}: {str(e)}"
        )]


async def main():
    """Run the MCP server."""
    async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):
        await server.run(
            read_stream,
            write_stream,
            server.create_initialization_options()
        )


if __name__ == "__main__":
    import asyncio
    asyncio.run(main())