Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 19,132 Bytes
61d29fc | 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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | #!/usr/bin/env python3
"""
Open Navigator MCP Server
==========================
Model Context Protocol (MCP) server for Open Navigator data sources.
Provides AI assistants access to:
- 90,000+ U.S. jurisdictions (Census data)
- 1.8M nonprofit organizations (IRS data)
- 4.5M+ legislative documents (Open States)
- Vector search across bills and meetings
- Real-time statistics and aggregates
Usage:
# Run locally
python scripts/mcp/open_navigator_server.py
# Configure in Claude Desktop (~/.config/Claude/claude_desktop_config.json):
{
"mcpServers": {
"open-navigator": {
"command": "python",
"args": ["/path/to/open-navigator/scripts/mcp/open_navigator_server.py"],
"env": {
"QDRANT_HOST": "localhost",
"QDRANT_PORT": "6333",
"DATABASE_URL": "postgresql://postgres:password@localhost:5433/open_navigator"
}
}
}
}
"""
import os
import sys
import json
import asyncio
from typing import Any, Optional
from pathlib import Path
# Add project root to path
project_root = Path(__file__).parent.parent.parent
sys.path.insert(0, str(project_root))
try:
from mcp.server import Server
from mcp.types import Tool, TextContent, Resource
except ImportError:
print("β MCP SDK not installed. Install with: pip install mcp anthropic-mcp-sdk")
sys.exit(1)
# Optional imports (graceful degradation)
try:
from qdrant_client import QdrantClient
QDRANT_AVAILABLE = True
except ImportError:
QDRANT_AVAILABLE = False
print("β οΈ Qdrant not available. Vector search tools disabled.")
try:
import psycopg2
from psycopg2.extras import RealDictCursor
POSTGRES_AVAILABLE = True
except ImportError:
POSTGRES_AVAILABLE = False
print("β οΈ PostgreSQL not available. Database tools disabled.")
try:
from datasets import load_dataset
DATASETS_AVAILABLE = True
except ImportError:
DATASETS_AVAILABLE = False
print("β οΈ HuggingFace datasets not available. Dataset tools disabled.")
# Initialize MCP server
app = Server("open-navigator")
# Initialize clients
qdrant_client = None
pg_conn = None
if QDRANT_AVAILABLE:
try:
qdrant_host = os.getenv("QDRANT_HOST", "localhost")
qdrant_port = int(os.getenv("QDRANT_PORT", "6333"))
qdrant_client = QdrantClient(host=qdrant_host, port=qdrant_port)
print(f"β
Connected to Qdrant at {qdrant_host}:{qdrant_port}")
except Exception as e:
print(f"β οΈ Failed to connect to Qdrant: {e}")
QDRANT_AVAILABLE = False
if POSTGRES_AVAILABLE:
try:
db_url = os.getenv("DATABASE_URL", "postgresql://postgres:password@localhost:5433/open_navigator")
pg_conn = psycopg2.connect(db_url, cursor_factory=RealDictCursor)
print(f"β
Connected to PostgreSQL")
except Exception as e:
print(f"β οΈ Failed to connect to PostgreSQL: {e}")
POSTGRES_AVAILABLE = False
@app.list_resources()
async def list_resources() -> list[Resource]:
"""List available datasets and collections"""
resources = []
if DATASETS_AVAILABLE:
resources.extend([
Resource(
uri="hf://census-jurisdictions",
name="U.S. Census Jurisdictions (90,000+)",
description="Cities, counties, and states with geographic data",
mimeType="application/x-parquet"
),
Resource(
uri="hf://nonprofits",
name="Nonprofit Organizations (1.8M)",
description="IRS-registered nonprofits with Form 990 data",
mimeType="application/x-parquet"
),
])
if QDRANT_AVAILABLE and qdrant_client:
try:
collections = qdrant_client.get_collections()
for coll in collections.collections:
resources.append(Resource(
uri=f"vector://{coll.name}",
name=f"{coll.name.title()} (Vector Search)",
description=f"Semantic search across {coll.points_count:,} documents",
mimeType="application/json"
))
except Exception as e:
print(f"β οΈ Error listing Qdrant collections: {e}")
return resources
@app.list_tools()
async def list_tools() -> list[Tool]:
"""List available tools"""
tools = []
# HuggingFace dataset tools
if DATASETS_AVAILABLE:
tools.extend([
Tool(
name="search_jurisdictions",
description="Search 90,000+ U.S. jurisdictions (cities, counties, states) by name, type, or location",
inputSchema={
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Search term (jurisdiction name)"
},
"state": {
"type": "string",
"description": "Filter by state code (e.g., CA, NY)"
},
"type": {
"type": "string",
"enum": ["city", "county", "state"],
"description": "Filter by jurisdiction type"
},
"limit": {
"type": "number",
"default": 10,
"description": "Maximum results to return"
}
},
"required": ["query"]
}
),
Tool(
name="get_nonprofits",
description="Get nonprofit organizations in a location with Form 990 data",
inputSchema={
"type": "object",
"properties": {
"state": {
"type": "string",
"description": "State code (e.g., CA, NY, TX)"
},
"city": {
"type": "string",
"description": "Filter by city name"
},
"subsection": {
"type": "string",
"description": "IRS subsection code (e.g., 03 for 501c3)"
},
"limit": {
"type": "number",
"default": 50,
"description": "Maximum results to return"
}
},
"required": ["state"]
}
),
])
# Vector search tools
if QDRANT_AVAILABLE and qdrant_client:
tools.extend([
Tool(
name="vector_search_bills",
description="Semantic search across legislative bills using natural language",
inputSchema={
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Natural language query"
},
"state": {
"type": "string",
"description": "Filter by state code"
},
"limit": {
"type": "number",
"default": 10,
"description": "Maximum results to return"
}
},
"required": ["query"]
}
),
Tool(
name="vector_search_meetings",
description="Semantic search across meeting transcripts using natural language",
inputSchema={
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Natural language query"
},
"municipality": {
"type": "string",
"description": "Filter by city/municipality name"
},
"limit": {
"type": "number",
"default": 10,
"description": "Maximum results to return"
}
},
"required": ["query"]
}
),
])
# PostgreSQL analytics tools
if POSTGRES_AVAILABLE and pg_conn:
tools.extend([
Tool(
name="get_bill_stats",
description="Get legislative statistics and aggregates by state/topic",
inputSchema={
"type": "object",
"properties": {
"state": {
"type": "string",
"description": "State code (e.g., CA, NY)"
},
"topic": {
"type": "string",
"description": "Bill topic/category"
}
}
}
),
Tool(
name="search_meetings",
description="Search meeting records by keyword, location, or date",
inputSchema={
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Search keyword"
},
"state": {
"type": "string",
"description": "Filter by state"
},
"limit": {
"type": "number",
"default": 20,
"description": "Maximum results to return"
}
}
}
),
])
return tools
@app.call_tool()
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
"""Execute a tool"""
# HuggingFace dataset tools
if name == "search_jurisdictions" and DATASETS_AVAILABLE:
try:
ds = load_dataset("getcommunityone/open-navigator-census", split="train")
# Filter by query
query = arguments["query"].lower()
results = ds.filter(lambda x: query in x["name"].lower())
# Filter by state
if arguments.get("state"):
state = arguments["state"].upper()
results = results.filter(lambda x: x.get("state_code") == state)
# Filter by type
if arguments.get("type"):
jtype = arguments["type"].lower()
results = results.filter(lambda x: jtype in x.get("type", "").lower())
# Limit results
limit = arguments.get("limit", 10)
results = results.select(range(min(limit, len(results))))
return [TextContent(
type="text",
text=json.dumps(results.to_pandas().to_dict('records'), indent=2, default=str)
)]
except Exception as e:
return [TextContent(type="text", text=f"Error: {str(e)}")]
elif name == "get_nonprofits" and DATASETS_AVAILABLE:
try:
state = arguments["state"].lower()
ds = load_dataset(f"getcommunityone/open-navigator-nonprofits-{state}", split="train")
df = ds.to_pandas()
# Filter by city
if arguments.get("city"):
city = arguments["city"].lower()
df = df[df['city'].str.lower().str.contains(city, na=False)]
# Filter by subsection
if arguments.get("subsection"):
df = df[df['subsection'] == arguments["subsection"]]
# Limit results
limit = arguments.get("limit", 50)
results = df.head(limit).to_dict('records')
return [TextContent(
type="text",
text=json.dumps(results, indent=2, default=str)
)]
except Exception as e:
return [TextContent(type="text", text=f"Error: {str(e)}")]
# Vector search tools
elif name == "vector_search_bills" and QDRANT_AVAILABLE and qdrant_client:
try:
query_filter = None
if arguments.get("state"):
query_filter = {
"must": [{"key": "state", "match": {"value": arguments["state"]}}]
}
results = qdrant_client.search(
collection_name="bills",
query_text=arguments["query"],
limit=arguments.get("limit", 10),
query_filter=query_filter
)
formatted_results = [{
"bill_id": r.payload.get("bill_id"),
"title": r.payload.get("title"),
"state": r.payload.get("state"),
"session": r.payload.get("session"),
"score": float(r.score),
"summary": r.payload.get("summary", "")[:200]
} for r in results]
return [TextContent(
type="text",
text=json.dumps(formatted_results, indent=2)
)]
except Exception as e:
return [TextContent(type="text", text=f"Error: {str(e)}")]
elif name == "vector_search_meetings" and QDRANT_AVAILABLE and qdrant_client:
try:
query_filter = None
if arguments.get("municipality"):
query_filter = {
"must": [{"key": "municipality", "match": {"value": arguments["municipality"]}}]
}
results = qdrant_client.search(
collection_name="meetings",
query_text=arguments["query"],
limit=arguments.get("limit", 10),
query_filter=query_filter
)
formatted_results = [{
"meeting_id": r.payload.get("meeting_id"),
"title": r.payload.get("title"),
"municipality": r.payload.get("municipality"),
"date": r.payload.get("date"),
"score": float(r.score),
"excerpt": r.payload.get("text", "")[:200]
} for r in results]
return [TextContent(
type="text",
text=json.dumps(formatted_results, indent=2)
)]
except Exception as e:
return [TextContent(type="text", text=f"Error: {str(e)}")]
# PostgreSQL tools
elif name == "get_bill_stats" and POSTGRES_AVAILABLE and pg_conn:
try:
cur = pg_conn.cursor()
if arguments.get("state"):
cur.execute("""
SELECT
state,
topic,
COUNT(*) as total_bills,
SUM(total_bills) as bill_count
FROM bills_map_aggregates
WHERE state = %s
GROUP BY state, topic
ORDER BY bill_count DESC
LIMIT 20
""", (arguments["state"],))
else:
cur.execute("""
SELECT
state,
COUNT(DISTINCT topic) as topics,
SUM(total_bills) as total_bills
FROM bills_map_aggregates
GROUP BY state
ORDER BY total_bills DESC
LIMIT 50
""")
results = cur.fetchall()
return [TextContent(
type="text",
text=json.dumps([dict(r) for r in results], indent=2, default=str)
)]
except Exception as e:
return [TextContent(type="text", text=f"Error: {str(e)}")]
elif name == "search_meetings" and POSTGRES_AVAILABLE and pg_conn:
try:
cur = pg_conn.cursor()
query = arguments.get("query", "")
state = arguments.get("state")
limit = arguments.get("limit", 20)
if state:
cur.execute("""
SELECT
name, organization_name, state, event_date,
description
FROM meetings
WHERE state = %s
AND (
name ILIKE %s
OR organization_name ILIKE %s
OR description ILIKE %s
)
ORDER BY event_date DESC
LIMIT %s
""", (state, f"%{query}%", f"%{query}%", f"%{query}%", limit))
else:
cur.execute("""
SELECT
name, organization_name, state, event_date,
description
FROM meetings
WHERE name ILIKE %s
OR organization_name ILIKE %s
OR description ILIKE %s
ORDER BY event_date DESC
LIMIT %s
""", (f"%{query}%", f"%{query}%", f"%{query}%", limit))
results = cur.fetchall()
return [TextContent(
type="text",
text=json.dumps([dict(r) for r in results], indent=2, default=str)
)]
except Exception as e:
return [TextContent(type="text", text=f"Error: {str(e)}")]
else:
return [TextContent(
type="text",
text=f"Unknown tool: {name}"
)]
async def main():
"""Run the MCP server"""
print("π Starting Open Navigator MCP Server...")
print(f" π HuggingFace Datasets: {'β
' if DATASETS_AVAILABLE else 'β'}")
print(f" π Qdrant Vector Search: {'β
' if QDRANT_AVAILABLE and qdrant_client else 'β'}")
print(f" πΎ PostgreSQL Analytics: {'β
' if POSTGRES_AVAILABLE and pg_conn else 'β'}")
print()
print("Ready to serve requests via MCP protocol")
# Run the server
async with app.run_async():
await asyncio.Event().wait()
if __name__ == "__main__":
asyncio.run(main())
|