|
| 1 | +from google.cloud import bigquery |
| 2 | +import logging |
| 3 | +from mcp.server.models import InitializationOptions |
| 4 | +import mcp.types as types |
| 5 | +from mcp.server import NotificationOptions, Server |
| 6 | +import mcp.server.stdio |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +# Set up logging to both stdout and file |
| 10 | +logger = logging.getLogger('mcp_bigquery_server') |
| 11 | +handler_stdout = logging.StreamHandler() |
| 12 | +handler_file = logging.FileHandler('/tmp/mcp_bigquery_server.log') |
| 13 | + |
| 14 | +# Set format for both handlers |
| 15 | +formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') |
| 16 | +handler_stdout.setFormatter(formatter) |
| 17 | +handler_file.setFormatter(formatter) |
| 18 | + |
| 19 | +# Add both handlers to logger |
| 20 | +logger.addHandler(handler_stdout) |
| 21 | +logger.addHandler(handler_file) |
| 22 | + |
| 23 | +# Set overall logging level |
| 24 | +logger.setLevel(logging.DEBUG) |
| 25 | + |
| 26 | +logger.info("Starting MCP BigQuery Server") |
| 27 | + |
| 28 | +class BigQueryDatabase: |
| 29 | + def __init__(self, project: str, location: str, datasets_filter: list[str]): |
| 30 | + """Initialize a BigQuery database client""" |
| 31 | + if not project: |
| 32 | + raise ValueError("Project is required") |
| 33 | + if not location: |
| 34 | + raise ValueError("Location is required") |
| 35 | + |
| 36 | + self.client = bigquery.Client(project=project, location=location) |
| 37 | + self.datasets_filter = datasets_filter |
| 38 | + |
| 39 | + def execute_query(self, query: str, params: dict[str, Any] | None = None) -> list[dict[str, Any]]: |
| 40 | + """Execute a SQL query and return results as a list of dictionaries""" |
| 41 | + logger.debug(f"Executing query: {query}") |
| 42 | + try: |
| 43 | + if params: |
| 44 | + job = self.client.query(query, job_config=bigquery.QueryJobConfig(query_parameters=params)) |
| 45 | + else: |
| 46 | + job = self.client.query(query) |
| 47 | + |
| 48 | + results = job.result() |
| 49 | + rows = [dict(row.items()) for row in results] |
| 50 | + logger.debug(f"Query returned {len(rows)} rows") |
| 51 | + return rows |
| 52 | + except Exception as e: |
| 53 | + logger.error(f"Database error executing query: {e}") |
| 54 | + raise |
| 55 | + |
| 56 | + def list_tables(self) -> list[str]: |
| 57 | + """List all tables in the BigQuery database""" |
| 58 | + logger.debug("Listing all tables") |
| 59 | + |
| 60 | + if self.datasets_filter: |
| 61 | + datasets = [self.client.dataset(dataset) for dataset in self.datasets_filter] |
| 62 | + else: |
| 63 | + datasets = list(self.client.list_datasets()) |
| 64 | + |
| 65 | + logger.debug(f"Found {len(datasets)} datasets") |
| 66 | + |
| 67 | + tables = [] |
| 68 | + for dataset in datasets: |
| 69 | + dataset_tables = self.client.list_tables(dataset.dataset_id) |
| 70 | + tables.extend([ |
| 71 | + f"{dataset.dataset_id}.{table.table_id}" for table in dataset_tables |
| 72 | + ]) |
| 73 | + |
| 74 | + logger.debug(f"Found {len(tables)} tables") |
| 75 | + return tables |
| 76 | + |
| 77 | + def describe_table(self, table_name: str) -> list[dict[str, Any]]: |
| 78 | + """Describe a table in the BigQuery database""" |
| 79 | + logger.debug(f"Describing table: {table_name}") |
| 80 | + |
| 81 | + parts = table_name.split(".") |
| 82 | + if len(parts) != 2: |
| 83 | + raise ValueError(f"Invalid table name: {table_name}") |
| 84 | + |
| 85 | + dataset_id = parts[0] |
| 86 | + table_id = parts[1] |
| 87 | + |
| 88 | + query = f""" |
| 89 | + SELECT ddl |
| 90 | + FROM {dataset_id}.INFORMATION_SCHEMA.TABLES |
| 91 | + WHERE table_name = @table_name; |
| 92 | + """ |
| 93 | + return self.execute_query(query, params=[ |
| 94 | + bigquery.ScalarQueryParameter("table_name", "STRING", table_id), |
| 95 | + ]) |
| 96 | + |
| 97 | +async def main(project: str, location: str, datasets_filter: list[str]): |
| 98 | + logger.info(f"Starting BigQuery MCP Server with project: {project} and location: {location}") |
| 99 | + |
| 100 | + db = BigQueryDatabase(project, location, datasets_filter) |
| 101 | + server = Server("bigquery-manager") |
| 102 | + |
| 103 | + # Register handlers |
| 104 | + logger.debug("Registering handlers") |
| 105 | + |
| 106 | + @server.list_tools() |
| 107 | + async def handle_list_tools() -> list[types.Tool]: |
| 108 | + """List available tools""" |
| 109 | + return [ |
| 110 | + types.Tool( |
| 111 | + name="execute-query", |
| 112 | + description="Execute a SELECT query on the BigQuery database", |
| 113 | + inputSchema={ |
| 114 | + "type": "object", |
| 115 | + "properties": { |
| 116 | + "query": {"type": "string", "description": "SELECT SQL query to execute using BigQuery dialect"}, |
| 117 | + }, |
| 118 | + "required": ["query"], |
| 119 | + }, |
| 120 | + ), |
| 121 | + types.Tool( |
| 122 | + name="list-tables", |
| 123 | + description="List all tables in the BigQuery database", |
| 124 | + inputSchema={ |
| 125 | + "type": "object", |
| 126 | + "properties": {}, |
| 127 | + }, |
| 128 | + ), |
| 129 | + types.Tool( |
| 130 | + name="describe-table", |
| 131 | + description="Get the schema information for a specific table", |
| 132 | + inputSchema={ |
| 133 | + "type": "object", |
| 134 | + "properties": { |
| 135 | + "table_name": {"type": "string", "description": "Name of the table to describe (e.g. my_dataset.my_table)"}, |
| 136 | + }, |
| 137 | + "required": ["table_name"], |
| 138 | + }, |
| 139 | + ), |
| 140 | + ] |
| 141 | + |
| 142 | + @server.call_tool() |
| 143 | + async def handle_call_tool( |
| 144 | + name: str, arguments: dict[str, Any] | None |
| 145 | + ) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]: |
| 146 | + """Handle tool execution requests""" |
| 147 | + logger.debug(f"Handling tool execution request: {name}") |
| 148 | + |
| 149 | + try: |
| 150 | + if name == "list-tables": |
| 151 | + results = db.list_tables() |
| 152 | + return [types.TextContent(type="text", text=str(results))] |
| 153 | + |
| 154 | + elif name == "describe-table": |
| 155 | + if not arguments or "table_name" not in arguments: |
| 156 | + raise ValueError("Missing table_name argument") |
| 157 | + results = db.describe_table(arguments["table_name"]) |
| 158 | + return [types.TextContent(type="text", text=str(results))] |
| 159 | + |
| 160 | + if name == "execute-query": |
| 161 | + results = db.execute_query(arguments["query"]) |
| 162 | + return [types.TextContent(type="text", text=str(results))] |
| 163 | + |
| 164 | + else: |
| 165 | + raise ValueError(f"Unknown tool: {name}") |
| 166 | + except Exception as e: |
| 167 | + return [types.TextContent(type="text", text=f"Error: {str(e)}")] |
| 168 | + |
| 169 | + async with mcp.server.stdio.stdio_server() as (read_stream, write_stream): |
| 170 | + logger.info("Server running with stdio transport") |
| 171 | + await server.run( |
| 172 | + read_stream, |
| 173 | + write_stream, |
| 174 | + InitializationOptions( |
| 175 | + server_name="bigquery", |
| 176 | + server_version="0.2.0", |
| 177 | + capabilities=server.get_capabilities( |
| 178 | + notification_options=NotificationOptions(), |
| 179 | + experimental_capabilities={}, |
| 180 | + ), |
| 181 | + ), |
| 182 | + ) |
0 commit comments