awslabs.redshift-mcp-server 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- awslabs/__init__.py +16 -0
- awslabs/redshift_mcp_server/__init__.py +17 -0
- awslabs/redshift_mcp_server/consts.py +136 -0
- awslabs/redshift_mcp_server/models.py +141 -0
- awslabs/redshift_mcp_server/redshift.py +630 -0
- awslabs/redshift_mcp_server/server.py +621 -0
- awslabs_redshift_mcp_server-0.0.1.dist-info/METADATA +432 -0
- awslabs_redshift_mcp_server-0.0.1.dist-info/RECORD +12 -0
- awslabs_redshift_mcp_server-0.0.1.dist-info/WHEEL +4 -0
- awslabs_redshift_mcp_server-0.0.1.dist-info/entry_points.txt +2 -0
- awslabs_redshift_mcp_server-0.0.1.dist-info/licenses/LICENSE +175 -0
- awslabs_redshift_mcp_server-0.0.1.dist-info/licenses/NOTICE +2 -0
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Redshift MCP Server implementation."""
|
|
16
|
+
|
|
17
|
+
import os
|
|
18
|
+
import sys
|
|
19
|
+
from awslabs.redshift_mcp_server.consts import (
|
|
20
|
+
CLIENT_BEST_PRACTICES,
|
|
21
|
+
DEFAULT_LOG_LEVEL,
|
|
22
|
+
REDSHIFT_BEST_PRACTICES,
|
|
23
|
+
)
|
|
24
|
+
from awslabs.redshift_mcp_server.models import (
|
|
25
|
+
QueryResult,
|
|
26
|
+
RedshiftCluster,
|
|
27
|
+
RedshiftColumn,
|
|
28
|
+
RedshiftDatabase,
|
|
29
|
+
RedshiftSchema,
|
|
30
|
+
RedshiftTable,
|
|
31
|
+
)
|
|
32
|
+
from awslabs.redshift_mcp_server.redshift import (
|
|
33
|
+
discover_clusters,
|
|
34
|
+
discover_columns,
|
|
35
|
+
discover_databases,
|
|
36
|
+
discover_schemas,
|
|
37
|
+
discover_tables,
|
|
38
|
+
execute_query,
|
|
39
|
+
)
|
|
40
|
+
from loguru import logger
|
|
41
|
+
from mcp.server.fastmcp import Context, FastMCP
|
|
42
|
+
from pydantic import Field
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
# Remove default handler and add custom configuration
|
|
46
|
+
logger.remove()
|
|
47
|
+
logger.add(
|
|
48
|
+
os.environ.get('LOG_FILE', sys.stderr),
|
|
49
|
+
level=os.environ.get('FASTMCP_LOG_LEVEL', DEFAULT_LOG_LEVEL),
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
mcp = FastMCP(
|
|
54
|
+
'awslabs.redshift-mcp-server',
|
|
55
|
+
instructions=f"""
|
|
56
|
+
# Amazon Redshift MCP Server.
|
|
57
|
+
|
|
58
|
+
This MCP server provides comprehensive access to Amazon Redshift clusters and serverless workgroups.
|
|
59
|
+
|
|
60
|
+
## Available Tools
|
|
61
|
+
|
|
62
|
+
### list_clusters
|
|
63
|
+
Lists all available Redshift clusters and serverless workgroups in your AWS account.
|
|
64
|
+
This tool provides essential information needed to connect to and query your Redshift instances.
|
|
65
|
+
|
|
66
|
+
### list_databases
|
|
67
|
+
Lists all databases in a specified Redshift cluster.
|
|
68
|
+
This tool queries the SVV_REDSHIFT_DATABASES system view to discover available databases.
|
|
69
|
+
|
|
70
|
+
### list_schemas
|
|
71
|
+
Lists all schemas in a specified database within a Redshift cluster.
|
|
72
|
+
This tool queries the SVV_ALL_SCHEMAS system view to discover available schemas.
|
|
73
|
+
|
|
74
|
+
### list_tables
|
|
75
|
+
Lists all tables in a specified schema within a Redshift database.
|
|
76
|
+
This tool queries the SVV_ALL_TABLES system view to discover available tables.
|
|
77
|
+
|
|
78
|
+
### list_columns
|
|
79
|
+
Lists all columns in a specified table within a Redshift schema.
|
|
80
|
+
This tool queries the SVV_ALL_COLUMNS system view to discover available columns.
|
|
81
|
+
|
|
82
|
+
### execute_query
|
|
83
|
+
Executes SQL queries against a Redshift cluster or serverless workgroup.
|
|
84
|
+
This tool uses the Redshift Data API to run queries and return results.
|
|
85
|
+
|
|
86
|
+
## Getting Started
|
|
87
|
+
|
|
88
|
+
1. Ensure your AWS credentials are configured (via AWS_PROFILE or default credentials).
|
|
89
|
+
2. Use the list_clusters tool to discover available Redshift instances.
|
|
90
|
+
3. Note the cluster identifiers for use with other tools (coming in future milestones).
|
|
91
|
+
|
|
92
|
+
{CLIENT_BEST_PRACTICES}
|
|
93
|
+
{REDSHIFT_BEST_PRACTICES}
|
|
94
|
+
""",
|
|
95
|
+
dependencies=['boto3', 'loguru', 'pydantic', 'regex'],
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
@mcp.tool(name='list_clusters')
|
|
100
|
+
async def list_clusters_tool(ctx: Context) -> list[RedshiftCluster]:
|
|
101
|
+
"""List all available Amazon Redshift clusters and serverless workgroups.
|
|
102
|
+
|
|
103
|
+
This tool discovers and returns information about all Redshift clusters and serverless workgroups
|
|
104
|
+
in your AWS account, including their current status, connection details, and configuration.
|
|
105
|
+
|
|
106
|
+
## Usage Requirements
|
|
107
|
+
|
|
108
|
+
- Ensure your AWS credentials are properly configured (via AWS_PROFILE or default credentials).
|
|
109
|
+
- Required IAM permissions: redshift:DescribeClusters, redshift-serverless:ListWorkgroups, redshift-serverless:GetWorkgroup.
|
|
110
|
+
|
|
111
|
+
## Response Structure
|
|
112
|
+
|
|
113
|
+
Returns a list of RedshiftCluster objects with the following structure:
|
|
114
|
+
|
|
115
|
+
- identifier: Unique identifier for the cluster/workgroup.
|
|
116
|
+
- type: Type of cluster (provisioned or serverless).
|
|
117
|
+
- status: Current status of the cluster.
|
|
118
|
+
- database_name: Default database name.
|
|
119
|
+
- endpoint: Connection endpoint information.
|
|
120
|
+
- port: Connection port.
|
|
121
|
+
- vpc_id: VPC ID where the cluster resides.
|
|
122
|
+
- node_type: Node type (for provisioned clusters).
|
|
123
|
+
- number_of_nodes: Number of nodes (for provisioned clusters).
|
|
124
|
+
- creation_time: When the cluster was created.
|
|
125
|
+
- master_username: Master username for the cluster.
|
|
126
|
+
- publicly_accessible: Whether the cluster is publicly accessible.
|
|
127
|
+
- encrypted: Whether the cluster is encrypted.
|
|
128
|
+
- tags: Tags associated with the cluster.
|
|
129
|
+
|
|
130
|
+
## Usage Tips
|
|
131
|
+
|
|
132
|
+
1. Use this tool to discover available Redshift instances before attempting connections.
|
|
133
|
+
2. Note the cluster identifiers for use with other database tools.
|
|
134
|
+
3. Check the status field to ensure clusters are 'available' before querying.
|
|
135
|
+
4. Use the endpoint and port information for direct database connections if needed.
|
|
136
|
+
5. Consider the cluster type (provisioned vs serverless) when planning your queries.
|
|
137
|
+
|
|
138
|
+
## Interpretation Best Practices
|
|
139
|
+
|
|
140
|
+
1. Filter results by status to find only available clusters.
|
|
141
|
+
2. Use cluster identifiers as input for other Redshift tools.
|
|
142
|
+
3. Consider cluster configuration (node type, encryption) for performance planning.
|
|
143
|
+
4. Check tags for environment or team information to select appropriate clusters.
|
|
144
|
+
"""
|
|
145
|
+
try:
|
|
146
|
+
logger.info('Discovering Redshift clusters and serverless workgroups')
|
|
147
|
+
clusters_data = await discover_clusters()
|
|
148
|
+
|
|
149
|
+
# Convert to RedshiftCluster models
|
|
150
|
+
clusters = []
|
|
151
|
+
for cluster_data in clusters_data:
|
|
152
|
+
cluster = RedshiftCluster(**cluster_data)
|
|
153
|
+
clusters.append(cluster)
|
|
154
|
+
|
|
155
|
+
logger.info(f'Successfully retrieved {len(clusters)} clusters')
|
|
156
|
+
return clusters
|
|
157
|
+
|
|
158
|
+
except Exception as e:
|
|
159
|
+
logger.error(f'Error in list_clusters_tool: {str(e)}')
|
|
160
|
+
await ctx.error(f'Failed to list clusters: {str(e)}')
|
|
161
|
+
raise
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
@mcp.tool(name='list_databases')
|
|
165
|
+
async def list_databases_tool(
|
|
166
|
+
ctx: Context,
|
|
167
|
+
cluster_identifier: str = Field(
|
|
168
|
+
...,
|
|
169
|
+
description='The cluster identifier to query for databases. Must be a valid cluster identifier from the list_clusters tool.',
|
|
170
|
+
),
|
|
171
|
+
database_name: str = Field(
|
|
172
|
+
'dev',
|
|
173
|
+
description='The database to connect to for querying system views. Defaults to "dev".',
|
|
174
|
+
),
|
|
175
|
+
) -> list[RedshiftDatabase]:
|
|
176
|
+
"""List all databases in a specified Amazon Redshift cluster.
|
|
177
|
+
|
|
178
|
+
This tool queries the SVV_REDSHIFT_DATABASES system view to discover all databases
|
|
179
|
+
that the user has access to in the specified cluster, including local databases
|
|
180
|
+
and databases created from datashares.
|
|
181
|
+
|
|
182
|
+
## Usage Requirements
|
|
183
|
+
|
|
184
|
+
- Ensure your AWS credentials are properly configured (via AWS_PROFILE or default credentials).
|
|
185
|
+
- The cluster must be available and accessible.
|
|
186
|
+
- Required IAM permissions: redshift-data:ExecuteStatement, redshift-data:DescribeStatement, redshift-data:GetStatementResult.
|
|
187
|
+
- The user must have access to the specified database to query system views.
|
|
188
|
+
|
|
189
|
+
## Parameters
|
|
190
|
+
|
|
191
|
+
- cluster_identifier: The unique identifier of the Redshift cluster to query.
|
|
192
|
+
IMPORTANT: Use a valid cluster identifier from the list_clusters tool.
|
|
193
|
+
- database_name: The database to connect to for querying system views (defaults to 'dev').
|
|
194
|
+
|
|
195
|
+
## Response Structure
|
|
196
|
+
|
|
197
|
+
Returns a list of RedshiftDatabase objects with the following structure:
|
|
198
|
+
|
|
199
|
+
- database_name: The name of the database.
|
|
200
|
+
- database_owner: The database owner user ID.
|
|
201
|
+
- database_type: The type of database (local or shared).
|
|
202
|
+
- database_acl: Access control information (for internal use).
|
|
203
|
+
- database_options: The properties of the database.
|
|
204
|
+
- database_isolation_level: The isolation level (Snapshot Isolation or Serializable).
|
|
205
|
+
|
|
206
|
+
## Usage Tips
|
|
207
|
+
|
|
208
|
+
1. First use list_clusters to get valid cluster identifiers.
|
|
209
|
+
2. Ensure the cluster status is 'available' before querying databases.
|
|
210
|
+
3. Use the default database name unless you know a specific database exists.
|
|
211
|
+
4. Note database types to understand if they are local or shared from datashares.
|
|
212
|
+
|
|
213
|
+
## Interpretation Best Practices
|
|
214
|
+
|
|
215
|
+
1. Focus on 'local' database types for cluster-native databases.
|
|
216
|
+
2. 'shared' database types indicate databases from datashares.
|
|
217
|
+
3. Use database names for subsequent schema and table discovery.
|
|
218
|
+
4. Consider database isolation levels for transaction planning.
|
|
219
|
+
"""
|
|
220
|
+
try:
|
|
221
|
+
logger.info(f'Discovering databases on cluster: {cluster_identifier}')
|
|
222
|
+
databases_data = await discover_databases(cluster_identifier, database_name)
|
|
223
|
+
|
|
224
|
+
# Convert to RedshiftDatabase models
|
|
225
|
+
databases = []
|
|
226
|
+
for database_data in databases_data:
|
|
227
|
+
database = RedshiftDatabase(**database_data)
|
|
228
|
+
databases.append(database)
|
|
229
|
+
|
|
230
|
+
logger.info(
|
|
231
|
+
f'Successfully retrieved {len(databases)} databases from cluster {cluster_identifier}'
|
|
232
|
+
)
|
|
233
|
+
return databases
|
|
234
|
+
|
|
235
|
+
except Exception as e:
|
|
236
|
+
logger.error(f'Error in list_databases_tool: {str(e)}')
|
|
237
|
+
await ctx.error(f'Failed to list databases on cluster {cluster_identifier}: {str(e)}')
|
|
238
|
+
raise
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
@mcp.tool(name='list_schemas')
|
|
242
|
+
async def list_schemas_tool(
|
|
243
|
+
ctx: Context,
|
|
244
|
+
cluster_identifier: str = Field(
|
|
245
|
+
...,
|
|
246
|
+
description='The cluster identifier to query for schemas. Must be a valid cluster identifier from the list_clusters tool.',
|
|
247
|
+
),
|
|
248
|
+
schema_database_name: str = Field(
|
|
249
|
+
...,
|
|
250
|
+
description='The database name to list schemas for. Also used to connect to. Must be a valid database name from the list_databases tool.',
|
|
251
|
+
),
|
|
252
|
+
) -> list[RedshiftSchema]:
|
|
253
|
+
"""List all schemas in a specified database within a Redshift cluster.
|
|
254
|
+
|
|
255
|
+
This tool queries the SVV_ALL_SCHEMAS system view to discover all schemas
|
|
256
|
+
that the user has access to in the specified database, including local schemas,
|
|
257
|
+
external schemas, and shared schemas from datashares.
|
|
258
|
+
|
|
259
|
+
## Usage Requirements
|
|
260
|
+
|
|
261
|
+
- Ensure your AWS credentials are properly configured (via AWS_PROFILE or default credentials).
|
|
262
|
+
- The cluster must be available and accessible.
|
|
263
|
+
- Required IAM permissions: redshift-data:ExecuteStatement, redshift-data:DescribeStatement, redshift-data:GetStatementResult.
|
|
264
|
+
- The user must have access to the database to query system views.
|
|
265
|
+
|
|
266
|
+
## Parameters
|
|
267
|
+
|
|
268
|
+
- cluster_identifier: The unique identifier of the Redshift cluster to query.
|
|
269
|
+
IMPORTANT: Use a valid cluster identifier from the list_clusters tool.
|
|
270
|
+
- schema_database_name: The database name to list schemas for. Also used to connect to.
|
|
271
|
+
IMPORTANT: Use a valid database name from the list_databases tool.
|
|
272
|
+
|
|
273
|
+
## Response Structure
|
|
274
|
+
|
|
275
|
+
Returns a list of RedshiftSchema objects with the following structure:
|
|
276
|
+
|
|
277
|
+
- database_name: The name of the database where the schema exists.
|
|
278
|
+
- schema_name: The name of the schema.
|
|
279
|
+
- schema_owner: The user ID of the schema owner.
|
|
280
|
+
- schema_type: The type of the schema (external, local, or shared).
|
|
281
|
+
- schema_acl: The permissions for the specified user or user group for the schema.
|
|
282
|
+
- source_database: The name of the source database for external schema.
|
|
283
|
+
- schema_option: The options of the schema (external schema attribute).
|
|
284
|
+
|
|
285
|
+
## Usage Tips
|
|
286
|
+
|
|
287
|
+
1. First use list_clusters to get valid cluster identifiers.
|
|
288
|
+
2. Then use list_databases to get valid database names for the cluster.
|
|
289
|
+
3. Ensure the cluster status is 'available' before querying schemas.
|
|
290
|
+
4. Note schema types to understand if they are local, external, or shared.
|
|
291
|
+
5. External schemas connect to external data sources like S3 or other databases.
|
|
292
|
+
|
|
293
|
+
## Interpretation Best Practices
|
|
294
|
+
|
|
295
|
+
1. Focus on 'local' schema types for cluster-native schemas.
|
|
296
|
+
2. 'external' schema types indicate connections to external data sources.
|
|
297
|
+
3. 'shared' schema types indicate schemas from datashares.
|
|
298
|
+
4. Use schema names for subsequent table and column discovery.
|
|
299
|
+
5. Consider schema permissions (schema_acl) for access planning.
|
|
300
|
+
"""
|
|
301
|
+
try:
|
|
302
|
+
logger.info(
|
|
303
|
+
f'Discovering schemas in database {schema_database_name} on cluster {cluster_identifier}'
|
|
304
|
+
)
|
|
305
|
+
schemas_data = await discover_schemas(cluster_identifier, schema_database_name)
|
|
306
|
+
|
|
307
|
+
# Convert to RedshiftSchema models
|
|
308
|
+
schemas = []
|
|
309
|
+
for schema_data in schemas_data:
|
|
310
|
+
schema = RedshiftSchema(**schema_data)
|
|
311
|
+
schemas.append(schema)
|
|
312
|
+
|
|
313
|
+
logger.info(
|
|
314
|
+
f'Successfully retrieved {len(schemas)} schemas from database {schema_database_name} on cluster {cluster_identifier}'
|
|
315
|
+
)
|
|
316
|
+
return schemas
|
|
317
|
+
|
|
318
|
+
except Exception as e:
|
|
319
|
+
logger.error(f'Error in list_schemas_tool: {str(e)}')
|
|
320
|
+
await ctx.error(
|
|
321
|
+
f'Failed to list schemas in database {schema_database_name} on cluster {cluster_identifier}: {str(e)}'
|
|
322
|
+
)
|
|
323
|
+
raise
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
@mcp.tool(name='list_tables')
|
|
327
|
+
async def list_tables_tool(
|
|
328
|
+
ctx: Context,
|
|
329
|
+
cluster_identifier: str = Field(
|
|
330
|
+
...,
|
|
331
|
+
description='The cluster identifier to query for tables. Must be a valid cluster identifier from the list_clusters tool.',
|
|
332
|
+
),
|
|
333
|
+
table_database_name: str = Field(
|
|
334
|
+
...,
|
|
335
|
+
description='The database name to list tables for. Must be a valid database name from the list_databases tool.',
|
|
336
|
+
),
|
|
337
|
+
table_schema_name: str = Field(
|
|
338
|
+
...,
|
|
339
|
+
description='The schema name to list tables for. Also used to connect to. Must be a valid schema name from the list_schemas tool.',
|
|
340
|
+
),
|
|
341
|
+
) -> list[RedshiftTable]:
|
|
342
|
+
"""List all tables in a specified schema within a Redshift database.
|
|
343
|
+
|
|
344
|
+
This tool queries the SVV_ALL_TABLES system view to discover all tables
|
|
345
|
+
that the user has access to in the specified schema, including base tables,
|
|
346
|
+
views, external tables, and shared tables.
|
|
347
|
+
|
|
348
|
+
## Usage Requirements
|
|
349
|
+
|
|
350
|
+
- Ensure your AWS credentials are properly configured (via AWS_PROFILE or default credentials).
|
|
351
|
+
- The cluster must be available and accessible.
|
|
352
|
+
- Required IAM permissions: redshift-data:ExecuteStatement, redshift-data:DescribeStatement, redshift-data:GetStatementResult.
|
|
353
|
+
- The user must have access to the database to query system views.
|
|
354
|
+
|
|
355
|
+
## Parameters
|
|
356
|
+
|
|
357
|
+
- cluster_identifier: The unique identifier of the Redshift cluster to query.
|
|
358
|
+
IMPORTANT: Use a valid cluster identifier from the list_clusters tool.
|
|
359
|
+
- table_database_name: The database name to list tables for.
|
|
360
|
+
IMPORTANT: Use a valid database name from the list_databases tool.
|
|
361
|
+
- table_schema_name: The schema name to list tables for.
|
|
362
|
+
IMPORTANT: Use a valid schema name from the list_schemas tool.
|
|
363
|
+
|
|
364
|
+
## Response Structure
|
|
365
|
+
|
|
366
|
+
Returns a list of RedshiftTable objects with the following structure:
|
|
367
|
+
|
|
368
|
+
- database_name: The name of the database where the table exists.
|
|
369
|
+
- schema_name: The schema name for the table.
|
|
370
|
+
- table_name: The name of the table.
|
|
371
|
+
- table_acl: The permissions for the specified user or user group for the table.
|
|
372
|
+
- table_type: The type of the table (views, base tables, external tables, shared tables).
|
|
373
|
+
- remarks: Remarks about the table.
|
|
374
|
+
|
|
375
|
+
## Usage Tips
|
|
376
|
+
|
|
377
|
+
1. First use list_clusters to get valid cluster identifiers.
|
|
378
|
+
2. Then use list_databases to get valid database names for the cluster.
|
|
379
|
+
3. Then use list_schemas to get valid schema names for the database.
|
|
380
|
+
4. Ensure the cluster status is 'available' before querying tables.
|
|
381
|
+
5. Note table types to understand if they are base tables, views, external tables, or shared tables.
|
|
382
|
+
|
|
383
|
+
## Interpretation Best Practices
|
|
384
|
+
|
|
385
|
+
1. Focus on 'TABLE' table types for regular database tables.
|
|
386
|
+
2. 'VIEW' table types indicate database views.
|
|
387
|
+
3. 'EXTERNAL TABLE' types indicate connections to external data sources.
|
|
388
|
+
4. 'SHARED TABLE' types indicate tables from datashares.
|
|
389
|
+
5. Use table names for subsequent column discovery and query operations.
|
|
390
|
+
6. Consider table permissions (table_acl) for access planning.
|
|
391
|
+
"""
|
|
392
|
+
try:
|
|
393
|
+
logger.info(
|
|
394
|
+
f'Discovering tables in schema {table_schema_name} in database {table_database_name} on cluster {cluster_identifier}'
|
|
395
|
+
)
|
|
396
|
+
tables_data = await discover_tables(
|
|
397
|
+
cluster_identifier, table_database_name, table_schema_name
|
|
398
|
+
)
|
|
399
|
+
|
|
400
|
+
# Convert to RedshiftTable models
|
|
401
|
+
tables = []
|
|
402
|
+
for table_data in tables_data:
|
|
403
|
+
table = RedshiftTable(**table_data)
|
|
404
|
+
tables.append(table)
|
|
405
|
+
|
|
406
|
+
logger.info(
|
|
407
|
+
f'Successfully retrieved {len(tables)} tables from schema {table_schema_name} in database {table_database_name} on cluster {cluster_identifier}'
|
|
408
|
+
)
|
|
409
|
+
return tables
|
|
410
|
+
|
|
411
|
+
except Exception as e:
|
|
412
|
+
logger.error(f'Error in list_tables_tool: {str(e)}')
|
|
413
|
+
await ctx.error(
|
|
414
|
+
f'Failed to list tables in schema {table_schema_name} in database {table_database_name} on cluster {cluster_identifier}: {str(e)}'
|
|
415
|
+
)
|
|
416
|
+
raise
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
@mcp.tool(name='list_columns')
|
|
420
|
+
async def list_columns_tool(
|
|
421
|
+
ctx: Context,
|
|
422
|
+
cluster_identifier: str = Field(
|
|
423
|
+
...,
|
|
424
|
+
description='The cluster identifier to query for columns. Must be a valid cluster identifier from the list_clusters tool.',
|
|
425
|
+
),
|
|
426
|
+
column_database_name: str = Field(
|
|
427
|
+
...,
|
|
428
|
+
description='The database name to list columns for. Must be a valid database name from the list_databases tool.',
|
|
429
|
+
),
|
|
430
|
+
column_schema_name: str = Field(
|
|
431
|
+
...,
|
|
432
|
+
description='The schema name to list columns for. Must be a valid schema name from the list_schemas tool.',
|
|
433
|
+
),
|
|
434
|
+
column_table_name: str = Field(
|
|
435
|
+
...,
|
|
436
|
+
description='The table name to list columns for. Must be a valid table name from the list_tables tool.',
|
|
437
|
+
),
|
|
438
|
+
) -> list[RedshiftColumn]:
|
|
439
|
+
"""List all columns in a specified table within a Redshift schema.
|
|
440
|
+
|
|
441
|
+
This tool queries the SVV_ALL_COLUMNS system view to discover all columns
|
|
442
|
+
that the user has access to in the specified table, including detailed information
|
|
443
|
+
about data types, constraints, and column properties.
|
|
444
|
+
|
|
445
|
+
## Usage Requirements
|
|
446
|
+
|
|
447
|
+
- Ensure your AWS credentials are properly configured (via AWS_PROFILE or default credentials).
|
|
448
|
+
- The cluster must be available and accessible.
|
|
449
|
+
- Required IAM permissions: redshift-data:ExecuteStatement, redshift-data:DescribeStatement, redshift-data:GetStatementResult.
|
|
450
|
+
- The user must have access to the database to query system views.
|
|
451
|
+
|
|
452
|
+
## Parameters
|
|
453
|
+
|
|
454
|
+
- cluster_identifier: The unique identifier of the Redshift cluster to query.
|
|
455
|
+
IMPORTANT: Use a valid cluster identifier from the list_clusters tool.
|
|
456
|
+
- column_database_name: The database name to list columns for.
|
|
457
|
+
IMPORTANT: Use a valid database name from the list_databases tool.
|
|
458
|
+
- column_schema_name: The schema name to list columns for.
|
|
459
|
+
IMPORTANT: Use a valid schema name from the list_schemas tool.
|
|
460
|
+
- column_table_name: The table name to list columns for.
|
|
461
|
+
IMPORTANT: Use a valid table name from the list_tables tool.
|
|
462
|
+
|
|
463
|
+
## Response Structure
|
|
464
|
+
|
|
465
|
+
Returns a list of RedshiftColumn objects with the following structure:
|
|
466
|
+
|
|
467
|
+
- database_name: The name of the database.
|
|
468
|
+
- schema_name: The name of the schema.
|
|
469
|
+
- table_name: The name of the table.
|
|
470
|
+
- column_name: The name of the column.
|
|
471
|
+
- ordinal_position: The position of the column in the table.
|
|
472
|
+
- column_default: The default value of the column.
|
|
473
|
+
- is_nullable: Whether the column is nullable (yes or no).
|
|
474
|
+
- data_type: The data type of the column.
|
|
475
|
+
- character_maximum_length: The maximum number of characters in the column.
|
|
476
|
+
- numeric_precision: The numeric precision.
|
|
477
|
+
- numeric_scale: The numeric scale.
|
|
478
|
+
- remarks: Remarks about the column.
|
|
479
|
+
|
|
480
|
+
## Usage Tips
|
|
481
|
+
|
|
482
|
+
1. First use list_clusters to get valid cluster identifiers.
|
|
483
|
+
2. Then use list_databases to get valid database names for the cluster.
|
|
484
|
+
3. Then use list_schemas to get valid schema names for the database.
|
|
485
|
+
4. Then use list_tables to get valid table names for the schema.
|
|
486
|
+
5. Ensure the cluster status is 'available' before querying columns.
|
|
487
|
+
6. Note data types and constraints for query planning and data validation.
|
|
488
|
+
|
|
489
|
+
## Interpretation Best Practices
|
|
490
|
+
|
|
491
|
+
1. Use ordinal_position to understand column order in the table.
|
|
492
|
+
2. Check is_nullable for required vs optional fields.
|
|
493
|
+
3. Use data_type information for proper data handling in queries.
|
|
494
|
+
4. Consider character_maximum_length for string field validation.
|
|
495
|
+
5. Use numeric_precision and numeric_scale for numeric field handling.
|
|
496
|
+
6. Use column names for SELECT statements and query construction.
|
|
497
|
+
"""
|
|
498
|
+
try:
|
|
499
|
+
logger.info(
|
|
500
|
+
f'Discovering columns in table {column_table_name} in schema {column_schema_name} in database {column_database_name} on cluster {cluster_identifier}'
|
|
501
|
+
)
|
|
502
|
+
columns_data = await discover_columns(
|
|
503
|
+
cluster_identifier, column_database_name, column_schema_name, column_table_name
|
|
504
|
+
)
|
|
505
|
+
|
|
506
|
+
# Convert to RedshiftColumn models
|
|
507
|
+
columns = []
|
|
508
|
+
for column_data in columns_data:
|
|
509
|
+
column = RedshiftColumn(**column_data)
|
|
510
|
+
columns.append(column)
|
|
511
|
+
|
|
512
|
+
logger.info(
|
|
513
|
+
f'Successfully retrieved {len(columns)} columns from table {column_table_name} in schema {column_schema_name} in database {column_database_name} on cluster {cluster_identifier}'
|
|
514
|
+
)
|
|
515
|
+
return columns
|
|
516
|
+
|
|
517
|
+
except Exception as e:
|
|
518
|
+
logger.error(f'Error in list_columns_tool: {str(e)}')
|
|
519
|
+
await ctx.error(
|
|
520
|
+
f'Failed to list columns in table {column_table_name} in schema {column_schema_name} in database {column_database_name} on cluster {cluster_identifier}: {str(e)}'
|
|
521
|
+
)
|
|
522
|
+
raise
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
@mcp.tool(name='execute_query')
|
|
526
|
+
async def execute_query_tool(
|
|
527
|
+
ctx: Context,
|
|
528
|
+
cluster_identifier: str = Field(
|
|
529
|
+
...,
|
|
530
|
+
description='The cluster identifier to execute the query on. Must be a valid cluster identifier from the list_clusters tool.',
|
|
531
|
+
),
|
|
532
|
+
database_name: str = Field(
|
|
533
|
+
...,
|
|
534
|
+
description='The database name to execute the query against. Must be a valid database name from the list_databases tool.',
|
|
535
|
+
),
|
|
536
|
+
sql: str = Field(
|
|
537
|
+
..., description='The SQL statement to execute. Should be a single SQL statement.'
|
|
538
|
+
),
|
|
539
|
+
) -> QueryResult:
|
|
540
|
+
"""Execute a SQL query against a Redshift cluster or serverless workgroup.
|
|
541
|
+
|
|
542
|
+
This tool uses the Redshift Data API to execute SQL queries and return results.
|
|
543
|
+
It supports both provisioned clusters and serverless workgroups, and handles
|
|
544
|
+
various data types in the result set.
|
|
545
|
+
|
|
546
|
+
## Usage Requirements
|
|
547
|
+
|
|
548
|
+
- Ensure your AWS credentials are properly configured (via AWS_PROFILE or default credentials).
|
|
549
|
+
- The cluster must be available and accessible.
|
|
550
|
+
- Required IAM permissions: redshift-data:ExecuteStatement, redshift-data:DescribeStatement, redshift-data:GetStatementResult.
|
|
551
|
+
- The user must have appropriate permissions to execute queries in the specified database.
|
|
552
|
+
|
|
553
|
+
## Parameters
|
|
554
|
+
|
|
555
|
+
- cluster_identifier: The unique identifier of the Redshift cluster to query.
|
|
556
|
+
IMPORTANT: Use a valid cluster identifier from the list_clusters tool.
|
|
557
|
+
- database_name: The database name to execute the query against.
|
|
558
|
+
IMPORTANT: Use a valid database name from the list_databases tool.
|
|
559
|
+
- sql: The SQL statement to execute. Should be a single SQL statement.
|
|
560
|
+
|
|
561
|
+
## Response Structure
|
|
562
|
+
|
|
563
|
+
Returns a QueryResult object with the following structure:
|
|
564
|
+
|
|
565
|
+
- columns: List of column names in the result set.
|
|
566
|
+
- rows: List of rows, where each row is a list of values.
|
|
567
|
+
- row_count: Number of rows returned.
|
|
568
|
+
- execution_time_ms: Query execution time in milliseconds.
|
|
569
|
+
- query_id: Unique identifier for the query execution.
|
|
570
|
+
|
|
571
|
+
## Usage Tips
|
|
572
|
+
|
|
573
|
+
1. First use list_clusters to get valid cluster identifiers.
|
|
574
|
+
2. Then use list_databases to get valid database names for the cluster.
|
|
575
|
+
3. Ensure the cluster status is 'available' before executing queries.
|
|
576
|
+
4. Use LIMIT clauses for exploratory queries to avoid large result sets.
|
|
577
|
+
5. Consider using the metadata discovery tools to understand table structures before querying.
|
|
578
|
+
|
|
579
|
+
## Data Type Handling
|
|
580
|
+
|
|
581
|
+
The tool automatically handles various Redshift data types:
|
|
582
|
+
- String values (VARCHAR, CHAR, TEXT).
|
|
583
|
+
- Numeric values (INTEGER, BIGINT, DECIMAL, FLOAT).
|
|
584
|
+
- Boolean values.
|
|
585
|
+
- NULL values.
|
|
586
|
+
- Date and timestamp values (returned as strings).
|
|
587
|
+
|
|
588
|
+
## Security Considerations
|
|
589
|
+
|
|
590
|
+
- Avoid dynamic SQL construction with user input.
|
|
591
|
+
- Consider database object permissions.
|
|
592
|
+
- Currently, the execute_query tool runs the query in a READ ONLY transaction to prevent unintentional modifications.
|
|
593
|
+
- The READ WRITE mode will be added in the future versions with additional protection mechanisms.
|
|
594
|
+
"""
|
|
595
|
+
try:
|
|
596
|
+
logger.info(f'Executing query on cluster {cluster_identifier} in database {database_name}')
|
|
597
|
+
query_result_data = await execute_query(cluster_identifier, database_name, sql)
|
|
598
|
+
|
|
599
|
+
# Convert to QueryResult model
|
|
600
|
+
query_result = QueryResult(**query_result_data)
|
|
601
|
+
|
|
602
|
+
logger.info(
|
|
603
|
+
f'Successfully executed query on cluster {cluster_identifier}: {query_result.row_count} rows returned in {query_result.execution_time_ms}ms'
|
|
604
|
+
)
|
|
605
|
+
return query_result
|
|
606
|
+
|
|
607
|
+
except Exception as e:
|
|
608
|
+
logger.error(f'Error in execute_query_tool: {str(e)}')
|
|
609
|
+
await ctx.error(
|
|
610
|
+
f'Failed to execute query on cluster {cluster_identifier} in database {database_name}: {str(e)}'
|
|
611
|
+
)
|
|
612
|
+
raise
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
def main():
|
|
616
|
+
"""Run the MCP server with CLI argument support."""
|
|
617
|
+
mcp.run()
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
if __name__ == '__main__':
|
|
621
|
+
main()
|