forked from hluaguo/metabase-mcp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·664 lines (537 loc) · 22.2 KB
/
server.py
File metadata and controls
executable file
·664 lines (537 loc) · 22.2 KB
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#!/usr/bin/env python3
"""
Metabase FastMCP Server
A FastMCP server that provides tools to interact with Metabase databases,
execute queries, manage cards, and work with collections.
"""
import asyncio
import logging
import os
import time
from collections import defaultdict
from enum import Enum
from typing import Any
import httpx
from dotenv import load_dotenv
from fastmcp import FastMCP
# Load environment variables
load_dotenv()
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Get Metabase configuration from environment variables
METABASE_URL = os.getenv("METABASE_URL")
METABASE_USER_EMAIL = os.getenv("METABASE_USER_EMAIL")
METABASE_PASSWORD = os.getenv("METABASE_PASSWORD")
METABASE_API_KEY = os.getenv("METABASE_API_KEY")
if not METABASE_URL or (
not METABASE_API_KEY and (not METABASE_USER_EMAIL or not METABASE_PASSWORD)
):
raise ValueError(
"METABASE_URL is required, and either METABASE_API_KEY or both METABASE_USER_EMAIL and METABASE_PASSWORD must be provided"
)
# Authentication method enum
class AuthMethod(Enum):
SESSION = "session"
API_KEY = "api_key"
# Initialize FastMCP server
mcp = FastMCP(name="metabase-mcp")
class MetabaseClient:
"""HTTP client for Metabase API operations"""
def __init__(self):
self.base_url = METABASE_URL.rstrip("/")
self.session_token: str | None = None
self.api_key: str | None = METABASE_API_KEY
self.auth_method = AuthMethod.API_KEY if METABASE_API_KEY else AuthMethod.SESSION
self.client = httpx.AsyncClient(timeout=30.0)
logger.info(f"Using {self.auth_method.value} authentication method")
async def _get_headers(self) -> dict[str, str]:
"""Get appropriate authentication headers"""
headers = {"Content-Type": "application/json"}
if self.auth_method == AuthMethod.API_KEY and self.api_key:
headers["X-API-KEY"] = self.api_key
elif self.auth_method == AuthMethod.SESSION:
if not self.session_token:
await self._get_session_token()
if self.session_token:
headers["X-Metabase-Session"] = self.session_token
return headers
async def _get_session_token(self) -> str:
"""Get Metabase session token for email/password authentication"""
if self.auth_method == AuthMethod.API_KEY and self.api_key:
return self.api_key
if not METABASE_USER_EMAIL or not METABASE_PASSWORD:
raise ValueError("Email and password required for session authentication")
login_data = {"username": METABASE_USER_EMAIL, "password": METABASE_PASSWORD}
response = await self.client.post(f"{self.base_url}/api/session", json=login_data)
if response.status_code != 200:
error_data = response.json() if response.content else {}
raise Exception(f"Authentication failed: {response.status_code} - {error_data}")
session_data = response.json()
self.session_token = session_data.get("id")
logger.info("Successfully obtained session token")
return self.session_token
async def request(self, method: str, path: str, **kwargs) -> dict[str, Any]:
"""Make authenticated request to Metabase API"""
url = f"{self.base_url}/api{path}"
headers = await self._get_headers()
logger.debug(f"Making {method} request to {path}")
response = await self.client.request(method=method, url=url, headers=headers, **kwargs)
if not response.is_success:
error_data = response.json() if response.content else {}
error_message = (
f"API request failed with status {response.status_code}: {response.text}"
)
logger.warning(f"{error_message} - {error_data}")
raise Exception(error_message)
logger.debug(f"Successful response from {path}")
return response.json()
async def close(self):
"""Close the HTTP client"""
await self.client.aclose()
# Global client instance
metabase_client = MetabaseClient()
# Tool implementations
@mcp.tool
async def find_candidate_collections(
query: str,
limit_collections: int = 10
) -> dict[str, Any]:
"""
Find collections whose names contain the query words.
Simple collection name matching - fast and reliable.
Args:
query: Text to search for in collection names.
limit_collections: Max collections to return.
"""
try:
# Get all collections
collections = await metabase_client.request("GET", "/collection")
if not isinstance(collections, list):
return {
"query": query,
"collections": [],
"results": {"total_collections_searched": 0, "matched_collections": 0}
}
# Search collection names
search_term = query.strip().lower()
matched_collections = []
for collection in collections:
if not collection: # Skip None collections
continue
collection_name = (collection.get("name") or "").lower()
collection_desc = (collection.get("description") or "").lower()
# Check if query matches collection name or description
if (search_term in collection_name or
search_term in collection_desc):
matched_collections.append({
"collection_id": collection.get("id"),
"collection_name": collection.get("name"),
"description": collection.get("description"),
"parent_id": collection.get("parent_id"),
"archived": collection.get("archived", False)
})
# Sort by name and apply limit
matched_collections.sort(key=lambda x: x.get("collection_name", "").lower())
limited_collections = matched_collections[:limit_collections]
return {
"query": query,
"collections": limited_collections,
"results": {
"total_collections_searched": len(collections),
"matched_collections": len(matched_collections),
"returned_collections": len(limited_collections)
},
"note": "Collections matching query in name or description. Use search_cards_in_collections next."
}
except Exception as e:
logger.error(f"Error finding candidate collections: {e}")
raise
@mcp.tool
async def search_cards_in_collections(
query: str,
collection_ids: list[int],
limit: int = 25,
offset: int = 0
) -> dict[str, Any]:
"""
Search for cards within specific collections by getting all cards from each collection
and filtering by query in card names and descriptions.
Args:
query: Text to search for in card names and descriptions.
collection_ids: List of collection IDs to search within.
limit: Max cards to return (page size).
offset: Number of matches to skip (pagination).
"""
try:
search_term = query.strip().lower()
all_matches = []
# Get cards from each collection
for collection_id in collection_ids:
try:
# Use existing list_cards_by_collection logic
all_cards = await metabase_client.request("GET", "/card")
if isinstance(all_cards, list):
# Filter by collection_id
collection_cards = [
card for card in all_cards
if card.get("collection_id") == collection_id
]
# Search within these cards
for card in collection_cards:
card_name = (card.get("name") or "").lower()
card_description = (card.get("description") or "").lower()
# Check if search term is in name or description
if (search_term in card_name or
search_term in card_description):
# Keep only essential fields
matched_card = {
"id": card.get("id"),
"name": card.get("name"),
"description": card.get("description"),
"collection_id": card.get("collection_id"),
"updated_at": card.get("updated_at"),
"created_at": card.get("created_at")
}
all_matches.append(matched_card)
except Exception as e:
logger.warning(f"Error searching collection {collection_id}: {e}")
continue
# Sort by updated_at desc (most recent first)
all_matches.sort(key=lambda x: x.get("updated_at") or "", reverse=True)
total_found = len(all_matches)
# Apply pagination
paginated_matches = all_matches[offset:offset + limit]
return {
"query": query,
"collections_searched": collection_ids,
"pagination": {
"limit": limit,
"offset": offset,
"returned": len(paginated_matches),
"total_found": total_found,
"has_more": offset + limit < total_found,
},
"cards": paginated_matches,
"note": f"Searched {len(collection_ids)} collections for '{query}'. Found {total_found} matching cards."
}
except Exception as e:
logger.error(f"Error searching cards in collections: {e}")
raise
@mcp.tool
async def search_metabase(
query: str,
limit: int = 20,
models: list[str] | None = None,
archived: bool = False,
search_native_query: bool | None = None
) -> dict[str, Any]:
"""
Search for items in Metabase using the search API.
Args:
query: Search term to find in item names and descriptions
limit: Maximum number of results to return (default: 20)
models: List of item types to filter by (e.g., ["card", "dashboard", "collection"])
archived: Include archived items in results (default: False)
search_native_query: Search within native SQL queries (default: None)
"""
try:
# Build base query parameters
params = {
"q": query,
"limit": limit,
"archived": str(archived).lower()
}
# Add optional parameters
if search_native_query is True:
params["search_native_query"] = "true"
# Build query string to handle multiple models parameters
from urllib.parse import urlencode
if models is not None:
params["models"] = models
query_string = urlencode(params, doseq=True)
result = await metabase_client.request("GET", f"/search?{query_string}")
# Add search metadata to response
search_info = {
"query": query,
"limit": limit,
"models": models,
"total_results": len(result.get("data", []) if isinstance(result, dict) else result),
}
if isinstance(result, dict):
result["search_info"] = search_info
else:
result = {
"data": result,
"search_info": search_info
}
return result
except Exception as e:
logger.error(f"Error searching Metabase: {e}")
raise
@mcp.tool
async def list_databases() -> dict[str, Any]:
"""List all databases in Metabase"""
try:
result = await metabase_client.request("GET", "/database")
return result
except Exception as e:
logger.error(f"Error listing databases: {e}")
raise
@mcp.tool
async def list_cards() -> dict[str, Any]:
"""List all questions/cards in Metabase (WARNING: Large dataset - 1700+ cards, may timeout)"""
try:
result = await metabase_client.request("GET", "/card")
return result
except Exception as e:
logger.error(f"Error listing cards: {e}")
raise
@mcp.tool
async def list_cards_paginated(limit: int = 50, offset: int = 0, filter_type: str = "all") -> dict[str, Any]:
"""
List cards with pagination and filtering to avoid timeout issues
Args:
limit: Maximum number of cards to return (default: 50)
offset: Number of cards to skip (default: 0)
filter_type: Filter type - 'all', 'mine', 'bookmarked', 'archived' (default: 'all')
"""
try:
# Build query parameters
params = {}
if filter_type != "all":
params["f"] = filter_type
# Convert params to query string
query_string = "&".join([f"{k}={v}" for k, v in params.items()])
endpoint = f"/card?{query_string}" if query_string else "/card"
result = await metabase_client.request("GET", endpoint)
# Apply pagination manually since Metabase API doesn't support limit/offset for cards
if isinstance(result, list):
paginated_result = result[offset:offset + limit]
return {
"cards": paginated_result,
"pagination": {
"limit": limit,
"offset": offset,
"returned": len(paginated_result),
"total_available": len(result),
"has_more": offset + limit < len(result)
},
"filter": filter_type
}
else:
return result
except Exception as e:
logger.error(f"Error listing cards with pagination: {e}")
raise
@mcp.tool
async def list_cards_by_collection(collection_id: int) -> dict[str, Any]:
"""
List cards in a specific collection (smaller, focused dataset)
Args:
collection_id: ID of the collection to filter by
"""
try:
# Get all cards first, then filter by collection
# Note: Metabase API doesn't have direct collection filtering for cards
result = await metabase_client.request("GET", "/card")
if isinstance(result, list):
# Filter by collection_id
filtered_cards = [
card for card in result
if card.get("collection_id") == collection_id
]
return {
"cards": filtered_cards,
"collection_id": collection_id,
"count": len(filtered_cards),
"message": f"Found {len(filtered_cards)} cards in collection {collection_id}"
}
else:
return result
except Exception as e:
logger.error(f"Error listing cards by collection {collection_id}: {e}")
raise
@mcp.tool
async def execute_card(card_id: int, parameters: dict[str, Any] | None = None) -> dict[str, Any]:
"""Execute a Metabase question/card and get results"""
try:
payload = {}
if parameters:
payload["parameters"] = parameters
result = await metabase_client.request("POST", f"/card/{card_id}/query", json=payload)
return result
except Exception as e:
logger.error(f"Error executing card {card_id}: {e}")
raise
@mcp.tool
async def execute_query(
database_id: int, query: str, native_parameters: list[dict[str, Any]] | None = None
) -> dict[str, Any]:
"""Execute a SQL query against a Metabase database"""
try:
payload = {"database": database_id, "type": "native", "native": {"query": query}}
if native_parameters:
payload["native"]["parameters"] = native_parameters
result = await metabase_client.request("POST", "/dataset", json=payload)
return result
except Exception as e:
logger.error(f"Error executing query: {e}")
raise
@mcp.tool
async def create_card(
name: str,
database_id: int,
query: str,
description: str | None = None,
collection_id: int | None = None,
visualization_settings: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""Create a new question/card in Metabase"""
try:
payload = {
"name": name,
"database_id": database_id,
"dataset_query": {
"database": database_id,
"type": "native",
"native": {"query": query},
},
"display": "table",
"visualization_settings": visualization_settings or {},
}
if description:
payload["description"] = description
if collection_id is not None:
payload["collection_id"] = collection_id
result = await metabase_client.request("POST", "/card", json=payload)
return result
except Exception as e:
logger.error(f"Error creating card: {e}")
raise
@mcp.tool
async def list_collections() -> dict[str, Any]:
"""List all collections in Metabase"""
try:
result = await metabase_client.request("GET", "/collection")
return result
except Exception as e:
logger.error(f"Error listing collections: {e}")
raise
@mcp.tool
async def create_collection(
name: str,
description: str | None = None,
color: str | None = None,
parent_id: int | None = None,
) -> dict[str, Any]:
"""Create a new collection in Metabase"""
try:
payload = {"name": name}
if description:
payload["description"] = description
if color:
payload["color"] = color
if parent_id is not None:
payload["parent_id"] = parent_id
result = await metabase_client.request("POST", "/collection", json=payload)
return result
except Exception as e:
logger.error(f"Error creating collection: {e}")
raise
@mcp.tool
async def list_tables(database_id: int) -> str:
"""List all tables in a database with formatted markdown output"""
try:
result = await metabase_client.request("GET", f"/database/{database_id}/metadata")
# Extract tables from the metadata response
tables = result.get("tables", [])
# Format tables with only the requested fields: table_id, display_name, description, entity_type
formatted_tables = []
for table in tables:
table_info = {
"table_id": table.get("id"),
"display_name": table.get("display_name"),
"description": table.get("description") or "No description",
"entity_type": table.get("entity_type")
}
formatted_tables.append(table_info)
# Sort by display_name for better readability
formatted_tables.sort(key=lambda x: x.get("display_name", ""))
# Generate markdown output
markdown_output = f"# Tables in Database {database_id}\n\n"
markdown_output += f"**Total Tables:** {len(formatted_tables)}\n\n"
if not formatted_tables:
markdown_output += "*No tables found in this database.*\n"
return markdown_output
# Create markdown table
markdown_output += "| Table ID | Display Name | Description | Entity Type |\n"
markdown_output += "|----------|--------------|-------------|--------------|\n"
for table in formatted_tables:
table_id = table.get("table_id", "N/A")
display_name = table.get("display_name", "N/A")
description = table.get("description", "No description")
entity_type = table.get("entity_type", "N/A")
# Escape pipe characters in content to prevent table formatting issues
description = description.replace("|", "\\|")
display_name = display_name.replace("|", "\\|")
markdown_output += f"| {table_id} | {display_name} | {description} | {entity_type} |\n"
return markdown_output
except Exception as e:
logger.error(f"Error listing tables for database {database_id}: {e}")
raise
@mcp.tool
async def get_table_fields(table_id: int, limit: int = 20) -> dict[str, Any]:
"""Get all fields/columns in a table
Args:
table_id: The ID of the table
limit: Maximum number of fields to return (default: 20)
"""
try:
result = await metabase_client.request("GET", f"/table/{table_id}/query_metadata")
# Apply field limiting if limit > 0 and there are more fields than the limit
if limit > 0 and "fields" in result and len(result["fields"]) > limit:
total_fields = len(result["fields"])
result["fields"] = result["fields"][:limit]
result["_truncated"] = True
result["_total_fields"] = total_fields
result["_limit_applied"] = limit
return result
except Exception as e:
logger.error(f"Error getting table fields for table {table_id}: {e}")
raise
# Cleanup handler
async def cleanup():
"""Clean up resources on shutdown"""
await metabase_client.close()
def main():
"""Main entry point for the server"""
try:
# Support multiple transport methods
import sys
# Get host and port from environment variables
host = os.getenv("HOST", "0.0.0.0")
port = int(os.getenv("PORT", "8000"))
# Check for transport argument
transport = "stdio" # default
if "--sse" in sys.argv:
transport = "sse"
elif "--http" in sys.argv:
transport = "streamable-http"
elif "--stdio" in sys.argv:
transport = "stdio"
logger.info(f"Starting Metabase MCP server with {transport} transport")
if transport in ["sse", "streamable-http"]:
logger.info(f"Server will be available at http://{host}:{port}")
mcp.run(transport=transport, host=host, port=port)
else:
mcp.run(transport=transport)
except KeyboardInterrupt:
logger.info("Server shutdown requested")
except Exception as e:
logger.error(f"Server error: {e}")
raise
finally:
asyncio.run(cleanup())
if __name__ == "__main__":
main()