-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathdbtools-mcp-server.py
More file actions
1273 lines (1095 loc) · 52.2 KB
/
Copy pathdbtools-mcp-server.py
File metadata and controls
1273 lines (1095 loc) · 52.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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Copyright (c) 2025, Oracle and/or its affiliates.
Licensed under the Universal Permissive License v1.0 as shown at http://oss.oracle.com/licenses/upl.
"""
import os.path
import requests
import json
import oci
from oci.signer import Signer
from oci.resource_search.models import StructuredSearchDetails
from fastmcp import FastMCP
MODEL_NAME = os.getenv("MODEL_NAME", "MINILM_L12_V2")
MODEL_EMBEDDING_DIMENSION = int(os.getenv("MODEL_EMBEDDING_DIMENSION", "384"))
mcp = FastMCP("oci")
profile_name = os.getenv("PROFILE_NAME", "DEFAULT")
config = oci.config.from_file(profile_name=profile_name)
identity_client = oci.identity.IdentityClient(config)
search_client = oci.resource_search.ResourceSearchClient(config)
database_client = oci.database.DatabaseClient(config)
dbtools_client = oci.database_tools.DatabaseToolsClient(config)
vault_client = oci.vault.VaultsClient(config)
secrets_client = oci.secrets.SecretsClient(config)
ords_endpoint = dbtools_client.base_client._endpoint.replace("https://", "https://sql.")
object_storage_client = oci.object_storage.ObjectStorageClient(config)
auth_signer = Signer(
tenancy=config['tenancy'],
user=config['user'],
fingerprint=config['fingerprint'],
private_key_file_location=config['key_file'],
pass_phrase=config['pass_phrase']
)
tenancy_id = os.getenv("TENANCY_ID_OVERRIDE", config['tenancy'])
@mcp.tool()
def list_all_compartments() -> str:
"""List all compartments in a tenancy with clear formatting"""
compartments = identity_client.list_compartments(tenancy_id).data
compartments.append(identity_client.get_compartment(compartment_id=tenancy_id).data)
return str(compartments)
def get_compartment_by_name(compartment_name: str):
"""Internal function to get compartment by name with caching"""
compartments = identity_client.list_compartments(
compartment_id=tenancy_id,
compartment_id_in_subtree=True,
access_level="ACCESSIBLE",
lifecycle_state="ACTIVE"
)
compartments.data.append(identity_client.get_compartment(compartment_id=tenancy_id).data)
# Search for the compartment by name
for compartment in compartments.data:
if compartment.name.lower() == compartment_name.lower():
return compartment
return None
@mcp.tool()
def get_compartment_by_name_tool(name: str) -> str:
"""Return a compartment matching the provided name"""
compartment = get_compartment_by_name(name)
if compartment:
return str(compartment)
else:
return json.dumps({"error": f"Compartment '{name}' not found."})
@mcp.tool()
def list_autonomous_databases(compartment_name: str) -> str:
"""List all databases in a given compartment name"""
compartment = get_compartment_by_name(compartment_name)
if not compartment:
return json.dumps({"error": f"Compartment '{compartment_name}' not found. Use list_compartment_names() to see available compartments."})
databases = database_client.list_autonomous_databases(compartment_id=compartment.id).data
return str(databases)
@mcp.tool()
def list_all_databases() -> str:
"""List all databases in the tenancy"""
search_details = StructuredSearchDetails(
query="query autonomousdatabase, database, pluggabledatabase, mysqldbsystem resources",
type="Structured",
matching_context_type="NONE"
)
results = search_client.search_resources(search_details=search_details, tenant_id=config['tenancy']).data
return str(results)
@mcp.tool()
def list_dbtools_connection_tool(compartment_name: str) -> str:
"""List all dbtools connections in a given compartment"""
compartment = get_compartment_by_name(compartment_name)
if not compartment:
return json.dumps({"error": f"Compartment '{compartment_name}' not found. Use list_compartment_names() to see available compartments."})
connections = dbtools_client.list_database_tools_connections(compartment_id=compartment.id).data
return str(connections)
@mcp.tool()
def list_all_connections() -> str:
"""List all database connections across all compartments"""
search_details = StructuredSearchDetails(
query="query databasetoolsconnection resources",
type="Structured",
matching_context_type="NONE"
)
search_results = search_client.search_resources(search_details=search_details, tenant_id=config['tenancy']).data
if not hasattr(search_results, 'items'):
return json.dumps([])
# Get full details for each connection
detailed_results = []
for item in search_results.items:
try:
connection = dbtools_client.get_database_tools_connection(item.identifier).data
detailed_results.append(connection)
except Exception as e:
# If we can't get details for a connection, include error info
detailed_results.append({
"error": f"Error getting details for connection {item.display_name}: {str(e)}",
"search_result": item.identifier
})
return str(detailed_results)
@mcp.tool()
def get_dbtools_connection_by_name_tool(display_name: str) -> str:
"""Get a dbtools connection for a given connection name"""
search_details = StructuredSearchDetails(
query=f"query databasetoolsconnection resources where displayName =~ '{display_name}'",
type="Structured",
matching_context_type="NONE"
)
search_results = search_client.search_resources(search_details=search_details, tenant_id=config['tenancy']).data
if not hasattr(search_results, 'items') or len(search_results.items) == 0:
return json.dumps({
"error": f"No connection found with name '{display_name}'",
"suggestion": "Use list_all_connections() to see available connections"
})
# Get the first matching connection's OCID
connection_id = search_results.items[0].identifier
# Get the full connection details
connection = dbtools_client.get_database_tools_connection(connection_id).data
return str(connection)
def get_minimal_connection_by_name(dbtools_connection_display_name: str):
"""
Internal function to get minimal connection information from a display name.
Returns a dictionary with connection details like id, type, and connection string.
"""
search_details = StructuredSearchDetails(
query=f"query databasetoolsconnection resources return allAdditionalFields where displayName =~ '{dbtools_connection_display_name}'",
type="Structured",
matching_context_type="NONE"
)
try:
resp = search_client.search_resources(search_details=search_details, tenant_id=config['tenancy']).data
if not hasattr(resp, 'items') or len(resp.items) == 0:
return None
item = resp.items[0]
# Extract key information from the search result
connection_info = {
'id': item.identifier,
'display_name': item.display_name,
'time_created': item.time_created,
'compartment_id': item.compartment_id,
'lifecycle_state': item.lifecycle_state
}
# Extract additional fields if available
if hasattr(item, 'additional_details') and item.additional_details:
additional = item.additional_details
if isinstance(additional, dict):
connection_info.update({
'type': additional.get('type'),
'connection_string': additional.get('connectionString')
})
return connection_info
except Exception as e:
print(f"Error in get_minimal_connection_by_name: {str(e)}")
return None
def execute_sql_tool_by_connection_id(connection_id: str, sql_script: str, binds: list = None) -> str:
"""Internal function to execute a SQL script using a connection ID with optional bind variables"""
try:
execute_sql_endpoint = f"{ords_endpoint}/ords/{connection_id}/_/sql"
# Prepare the request payload
payload = {
"statementText": sql_script
}
if binds:
payload["binds"] = binds
response = requests.post(
execute_sql_endpoint,
json=payload,
auth=auth_signer,
headers={"Content-Type": "application/json"}
)
# Try to format JSON response if possible
try:
return json.dumps(response.json(), indent=2)
except:
return response.text
except Exception as e:
return json.dumps({
"error": f"Error executing SQL: {str(e)}",
"sql_script": sql_script,
"binds": binds
})
@mcp.tool()
def execute_sql_tool(dbtools_connection_display_name: str, sql_script: str) -> str:
"""Execute SQL statements on a dbtools connection. When
Substitition char & is used in a string value, it must be escaped like this: ''&''
WARNING: This tool can perform destructive operations on the database,
user permission must be explicitely requested by the client before executing.
"""
connection_info = get_minimal_connection_by_name(dbtools_connection_display_name)
if connection_info is None:
return json.dumps({
"error": f"No connection found with name '{dbtools_connection_display_name}'",
"suggestion": "Use list_all_connections() to see available connections"
})
return execute_sql_tool_by_connection_id(connection_info['id'], sql_script)
@mcp.tool()
def get_table_info(dbtools_connection_display_name: str, table_name: str) -> str:
"""
Get detailed schema information about a specific database table.
Supports ORACLE_DATABASE and MYSQL database types.
"""
connection_info = get_minimal_connection_by_name(dbtools_connection_display_name)
if connection_info is None:
return json.dumps({
"error": f"No connection found with name '{dbtools_connection_display_name}'",
"suggestion": "Use list_all_connections() to see available connections"
})
try:
# Get database type from the connection info
db_type = connection_info.get('type')
if db_type == 'ORACLE_DATABASE':
# First try with all_tab_columns (includes system views)
column_sql = f"""
WITH pk_columns AS (
SELECT column_name
FROM all_cons_columns acc
JOIN all_constraints ac ON acc.constraint_name = ac.constraint_name
AND acc.owner = ac.owner
WHERE ac.table_name = '{table_name.upper()}'
AND ac.constraint_type = 'P'
)
SELECT
c.column_name,
c.data_type,
c.data_length,
c.nullable,
c.data_default,
cc.comments,
CASE WHEN pk.column_name IS NOT NULL THEN 1 ELSE 0 END as is_primary_key,
t.num_rows
FROM all_tab_columns c
LEFT JOIN all_col_comments cc
ON cc.table_name = c.table_name
AND cc.column_name = c.column_name
AND cc.owner = c.owner
LEFT JOIN pk_columns pk
ON pk.column_name = c.column_name
LEFT JOIN all_tables t
ON t.table_name = c.table_name
AND t.owner = c.owner
WHERE c.table_name = '{table_name.upper()}'
ORDER BY c.column_id
"""
elif db_type == 'MYSQL':
column_sql = f"""
SELECT
c.column_name,
c.data_type,
c.character_maximum_length as data_length,
c.is_nullable as nullable,
c.column_default as data_default,
c.column_comment as comments,
CASE WHEN tc.constraint_type = 'PRIMARY KEY' THEN 1 ELSE 0 END as is_primary_key,
t.table_rows as num_rows
FROM information_schema.columns c
LEFT JOIN information_schema.key_column_usage kcu
ON kcu.table_schema = c.table_schema
AND kcu.table_name = c.table_name
AND kcu.column_name = c.column_name
LEFT JOIN information_schema.table_constraints tc
ON tc.table_schema = kcu.table_schema
AND tc.table_name = kcu.table_name
AND tc.constraint_name = kcu.constraint_name
LEFT JOIN information_schema.tables t
ON t.table_schema = c.table_schema
AND t.table_name = c.table_name
WHERE c.table_name = '{table_name}'
AND c.table_schema = database()
ORDER BY c.ordinal_position
"""
else:
return json.dumps({
"error": f"Unsupported database type: {db_type}",
"supported_types": ["ORACLE_DATABASE", "MYSQL"]
})
result = execute_sql_tool_by_connection_id(connection_info['id'], column_sql)
try:
raw_data = json.loads(result)
if not raw_data.get('items') or not raw_data['items'][0].get('resultSet'):
return json.dumps({
"error": "No data returned from query",
"raw_result": result
})
# Extract column data
columns = []
primary_keys = []
row_count = 0
for col in raw_data['items'][0]['resultSet'].get('items', []):
column = {
"name": col['column_name'],
"type": col['data_type'],
"length": int(col['data_length']) if col['data_length'] is not None else None,
"nullable": col['nullable'] == 'Y' if db_type == 'ORACLE_DATABASE' else col['nullable'] == 'YES',
"default": col['data_default'].strip() if col['data_default'] else None,
"comment": col['comments']
}
columns.append(column)
if col['is_primary_key'] == 1:
primary_keys.append(col['column_name'])
# Get row count from first row (it's the same for all rows)
if row_count == 0:
row_count = col['num_rows'] or 0
if len(columns) == 0:
return json.dumps({
"error": "No columns found for table",
"table": table_name
})
# Build the final response
response = {
"table_name": table_name.upper() if db_type == 'ORACLE_DATABASE' else table_name,
"columns": columns,
"primary_key": primary_keys,
"row_count": row_count
}
return json.dumps(response, indent=2)
except json.JSONDecodeError:
return json.dumps({
"error": "Failed to parse SQL response",
"raw_result": result
})
except Exception as e:
return json.dumps({
"error": f"Error processing results: {str(e)}",
"raw_result": result
})
except Exception as e:
return json.dumps({
"error": f"Error getting table info: {str(e)}",
"connection": dbtools_connection_display_name,
"table": table_name
})
@mcp.tool()
def list_tables(dbtools_connection_display_name: str) -> str:
"""
List all tables in the database with basic information (name, row count, comments).
Supports ORACLE_DATABASE and MYSQL database types.
"""
connection_info = get_minimal_connection_by_name(dbtools_connection_display_name)
if connection_info is None:
return json.dumps({
"error": f"No connection found with name '{dbtools_connection_display_name}'",
"suggestion": "Use list_all_connections() to see available connections"
})
try:
# Get database type from the connection info
db_type = connection_info.get('type')
if db_type == 'ORACLE_DATABASE':
sql_script = """
SELECT table_name, num_rows, comments
FROM user_tables
LEFT JOIN user_tab_comments USING (table_name)
ORDER BY table_name
"""
elif db_type == 'MYSQL':
sql_script = """
SELECT
table_name,
table_rows as num_rows,
table_comment as comments
FROM information_schema.tables
WHERE table_schema = database()
ORDER BY table_name
"""
else:
return json.dumps({
"error": f"Unsupported database type: {db_type}",
"supported_types": ["ORACLE_DATABASE", "MYSQL"]
})
# Execute SQL and get the response
response = execute_sql_tool_by_connection_id(connection_info['id'], sql_script)
# Parse the response to extract just the table items
try:
response_json = json.loads(response)
# Navigate to the items array in the complex response structure
if response_json['items'] and len(response_json['items']) > 0:
first_statement = response_json['items'][0]
if 'resultSet' in first_statement and 'items' in first_statement['resultSet']:
# Extract just the table items
tables = first_statement['resultSet']['items']
return json.dumps(tables, indent=2)
# If we couldn't find the expected structure, return an empty array
return json.dumps([])
except Exception as e:
return json.dumps({
"error": f"Error parsing SQL results: {str(e)}",
"raw_response": response[:200] + "..." if len(response) > 200 else response
})
except Exception as e:
return json.dumps({
"error": f"Error listing tables: {str(e)}",
"connection": dbtools_connection_display_name
})
@mcp.tool()
def bootstrap_reports(dbtools_connection_display_name: str) -> str:
"""
Ensures the 'report_definitions' table exists in the current user's schema.
Each database schema gets its own report_definitions table, allowing users
to manage their reports independently.
The table stores:
- name: Unique report identifier
- description: Optional report description
- time_created/updated: Timestamps
- sql_definition: JSON with SQL query and bind parameters
- text_vector: VECTOR(MODEL_EMBEDDING_DIMENSION) for semantic similarity search
Note: This tool operates only in the current user's schema, identified by
SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA'). It will not affect report_definitions
tables in other schemas.
Args:
dbtools_connection_display_name: The name of the database connection
Returns:
JSON string with:
- ok (bool): True if operation succeeded
- message (str): Human readable status message
- error (str, optional): Error message if operation failed
- step (str, optional): Step where error occurred
Example sql_definition format:
{
"sql": "select id from employees where hire_date = :hire_date and age > :age",
"binds": [
{"name": "hire_date"},
{"name": "age"}
]
}
"""
try:
connection_info = get_minimal_connection_by_name(dbtools_connection_display_name)
if connection_info is None:
return json.dumps({
"ok": False,
"error": f"No connection found with name '{dbtools_connection_display_name}'",
"step": "connection"
})
# Verify database type early
db_type = connection_info.get('type', 'ORACLE_DATABASE')
if db_type != 'ORACLE_DATABASE':
return json.dumps({
"ok": False,
"error": f"Unsupported database type: {db_type}. This tool only supports Oracle databases.",
"step": "validate_type"
})
# Check if table exists in current schema
check_sql = """
SELECT owner, table_name
FROM all_tables
WHERE table_name = 'REPORT_DEFINITIONS'
AND owner = SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')
"""
check_result = execute_sql_tool_by_connection_id(connection_info['id'], check_sql)
try:
check_data = json.loads(check_result)
result_set = check_data.get("items", [{}])[0].get("resultSet", {})
if result_set.get("items"):
schema = result_set["items"][0].get("owner", "current schema")
return json.dumps({
"ok": True,
"message": f"Table 'report_definitions' exists in schema {schema}"
})
except json.JSONDecodeError:
return json.dumps({
"ok": False,
"error": "Invalid response format while checking table existence",
"step": "check_table"
})
# Create table if it doesn't exist
ddl = """
CREATE TABLE report_definitions (
name VARCHAR(4000) PRIMARY KEY,
description VARCHAR(4000),
time_created TIMESTAMP(6),
time_updated TIMESTAMP(6),
sql_definition json,
text_vector VECTOR({MODEL_EMBEDDING_DIMENSION})
)
"""
create_resp = execute_sql_tool(dbtools_connection_display_name, ddl)
try:
create_data = json.loads(create_resp)
if "error" in create_data:
return json.dumps({
"ok": False,
"error": create_data["error"],
"step": "create_table"
})
current_schema = None
try:
# Get current schema for the success message
schema_sql = "SELECT SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') as schema FROM DUAL"
schema_result = execute_sql_tool_by_connection_id(connection_info['id'], schema_sql)
schema_data = json.loads(schema_result)
result_set = schema_data.get("items", [{}])[0].get("resultSet", {})
if result_set.get("items"):
current_schema = result_set["items"][0].get("schema")
except Exception:
pass
schema_msg = f" in schema {current_schema}" if current_schema else ""
return json.dumps({
"ok": True,
"message": f"Table 'report_definitions' created{schema_msg}"
})
except json.JSONDecodeError:
return json.dumps({
"ok": False,
"error": "Invalid response format while creating table",
"step": "create_table"
})
except Exception as e:
return json.dumps({
"ok": False,
"error": str(e),
"step": "unknown"
})
@mcp.tool()
def create_report(dbtools_connection_display_name: str, name: str, sql_query: str, description: str = None, bind_parameters: list = None) -> str:
"""
Create a new report definition in the report_definitions table.
Args:
dbtools_connection_display_name: The name of the database connection
name: Unique name for the report
sql_query: The SQL query to execute
description: Optional description of what the report does
bind_parameters: Optional list of bind parameter names, e.g. ["customer_id", "start_date"]
"""
connection_info = get_minimal_connection_by_name(dbtools_connection_display_name)
if connection_info is None:
return json.dumps({"error": f"No connection found with name '{dbtools_connection_display_name}'"})
# Check if table exists first
bootstrap_result = bootstrap_reports(dbtools_connection_display_name)
try:
result = json.loads(bootstrap_result)
if "error" in result:
return bootstrap_result
except:
return bootstrap_result
# Prepare the SQL definition JSON
sql_definition = {
"sql": sql_query
}
if bind_parameters:
sql_definition["binds"] = [{
"name": param
} for param in bind_parameters]
# Generate text embedding from name and description
text_to_embed = name
if description:
text_to_embed = f"{name}. {description}"
# Insert the new report using direct SQL to avoid bind variable issues
insert_sql = f"""
INSERT INTO report_definitions (
name,
description,
time_created,
time_updated,
sql_definition,
text_vector
) VALUES (
'{name}',
'{description or ""}',
SYSTIMESTAMP,
SYSTIMESTAMP,
'{json.dumps(sql_definition).replace("'", "''")}',
VECTOR_EMBEDDING({MODEL_NAME} USING '{text_to_embed.replace("'", "''") if text_to_embed else ""}' AS data)
)"""
result = execute_sql_tool_by_connection_id(connection_info['id'], insert_sql)
try:
# Check for errors
json_result = json.loads(result)
if "error" in json_result:
return json.dumps({
"error": "Failed to create report",
"details": json_result["error"]
})
except:
pass
return json.dumps({
"ok": True,
"execute_output": result,
"message": f"Report '{name}' created successfully",
"report": {
"name": name,
"description": description,
"sql_definition": sql_definition,
"text_to_embed": text_to_embed
}
})
@mcp.tool()
def execute_report(dbtools_connection_display_name: str, report_name: str, bind_values: dict = None) -> str:
"""
Execute a report by its name with optional bind parameter values.
Args:
dbtools_connection_display_name: The name of the database connection
report_name: Name of the report to execute
bind_values: Optional dictionary of bind parameter values, e.g. {"year": 2024, "rating": 8.5}
"""
connection_info = get_minimal_connection_by_name(dbtools_connection_display_name)
if connection_info is None:
return json.dumps({"error": f"No connection found with name '{dbtools_connection_display_name}'"})
# Get the report definition
get_report_sql = """
SELECT sql_definition
FROM report_definitions
WHERE name = :name"""
get_report_binds = [{"name": "name", "data_type": "VARCHAR", "value": report_name}]
result = execute_sql_tool_by_connection_id(connection_info['id'], get_report_sql, get_report_binds)
try:
json_result = json.loads(result)
if "error" in json_result:
return json.dumps({
"error": "Failed to get report definition",
"details": json_result["error"]
})
# Extract the first row's sql_definition
if "items" not in json_result or not json_result["items"]:
return json.dumps({"error": f"Report '{report_name}' not found"})
sql_definition = json_result["items"][0]["resultSet"]["items"][0]["sql_definition"]
except Exception as e:
return json.dumps({
"error": "Failed to parse report definition",
"details": str(e),
"response": result
})
# Prepare the binds if needed
binds = None
if "binds" in sql_definition and bind_values:
binds = []
for bind_def in sql_definition["binds"]:
bind_name = bind_def["name"]
if bind_name not in bind_values:
return json.dumps({
"error": f"Missing required bind parameter: {bind_name}",
"required_binds": [b["name"] for b in sql_definition["binds"]]
})
# Infer data type from the value
value = bind_values[bind_name]
if isinstance(value, int):
data_type = "NUMBER"
elif isinstance(value, float):
data_type = "NUMBER"
elif isinstance(value, str):
data_type = "VARCHAR"
else:
data_type = "VARCHAR"
value = str(value)
binds.append({
"name": bind_name,
"data_type": data_type,
"value": value
})
# Execute the report
return execute_sql_tool_by_connection_id(connection_info['id'], sql_definition["sql"], binds)
@mcp.tool()
def get_report(dbtools_connection_display_name: str, report_name: str) -> str:
"""
Retrieve a single report definition by name.
Returns the report's name, description, creation/update times, and SQL definition.
This is only supported for Oracle databases.
Args:
dbtools_connection_display_name: The name of the database connection
report_name: Name of the report to retrieve
"""
connection_info = get_minimal_connection_by_name(dbtools_connection_display_name)
if connection_info is None:
return json.dumps({"error": f"No connection found with name '{dbtools_connection_display_name}'"})
# Get the report definition
get_report_sql = """
SELECT
name,
description,
TO_CHAR(time_created, 'YYYY-MM-DD"T"HH24:MI:SS"Z"') as time_created,
TO_CHAR(time_updated, 'YYYY-MM-DD"T"HH24:MI:SS"Z"') as time_updated,
sql_definition
FROM report_definitions
WHERE name = :name"""
get_report_binds = [{"name": "name", "data_type": "VARCHAR", "value": report_name}]
result = execute_sql_tool_by_connection_id(connection_info['id'], get_report_sql, get_report_binds)
try:
json_result = json.loads(result)
if "error" in json_result:
return json.dumps({
"error": "Failed to get report definition",
"details": json_result["error"]
})
# Extract the first row
if "items" not in json_result or not json_result["items"]:
return json.dumps({"error": f"Report '{report_name}' not found"})
report = json_result["items"][0]["resultSet"]["items"][0]
sql_definition = report["sql_definition"]
return json.dumps({
"name": report["name"],
"description": report["description"],
"time_created": report["time_created"],
"time_updated": report["time_updated"],
"sql_query": sql_definition["sql"],
"bind_parameters": [bind["name"] for bind in sql_definition.get("binds", [])] if "binds" in sql_definition else None
}, indent=2)
except Exception as e:
return json.dumps({
"error": "Failed to parse report definition",
"details": str(e),
"response": result
})
@mcp.tool()
def delete_report(dbtools_connection_display_name: str, report_name: str) -> str:
"""
Delete a report definition by name.
This is only supported for Oracle databases.
Args:
dbtools_connection_display_name: The name of the database connection
report_name: Name of the report to delete
"""
connection_info = get_minimal_connection_by_name(dbtools_connection_display_name)
if connection_info is None:
return json.dumps({"error": f"No connection found with name '{dbtools_connection_display_name}'"})
# First check if the report exists
get_report_sql = """
SELECT name FROM report_definitions WHERE name = :name"""
get_report_binds = [{"name": "name", "data_type": "VARCHAR", "value": report_name}]
result = execute_sql_tool_by_connection_id(connection_info['id'], get_report_sql, get_report_binds)
try:
json_result = json.loads(result)
if "error" in json_result:
return json.dumps({
"error": "Failed to check report existence",
"details": json_result["error"]
})
if "items" not in json_result or not json_result["items"]:
return json.dumps({"error": f"Report '{report_name}' not found"})
except Exception as e:
return json.dumps({
"error": "Failed to check report existence",
"details": str(e)
})
# Delete the report
delete_sql = """
DELETE FROM report_definitions
WHERE name = :name"""
delete_binds = [{"name": "name", "data_type": "VARCHAR", "value": report_name}]
result = execute_sql_tool_by_connection_id(connection_info['id'], delete_sql, delete_binds)
try:
json_result = json.loads(result)
if "error" in json_result:
return json.dumps({
"error": "Failed to delete report",
"details": json_result["error"]
})
return json.dumps({
"ok": True,
"message": f"Report '{report_name}' deleted successfully"
})
except Exception as e:
return json.dumps({
"error": "Failed to delete report",
"details": str(e)
})
@mcp.tool()
def list_reports(dbtools_connection_display_name: str) -> str:
"""
List all reports from the report_definitions table.
Returns report name, description, and creation/update times.
This is only supported for Oracle databases.
"""
connection_info = get_minimal_connection_by_name(dbtools_connection_display_name)
if connection_info is None:
return json.dumps({"error": f"No connection found with name '{dbtools_connection_display_name}'"})
# Check if table exists first
bootstrap_result = bootstrap_reports(dbtools_connection_display_name)
try:
result = json.loads(bootstrap_result)
if "error" in result:
return bootstrap_result
except:
return bootstrap_result
# Query the reports
sql = """
SELECT
name,
description,
TO_CHAR(time_created, 'YYYY-MM-DD"T"HH24:MI:SS"Z"') as time_created,
TO_CHAR(time_updated, 'YYYY-MM-DD"T"HH24:MI:SS"Z"') as time_updated
FROM report_definitions
ORDER BY name
"""
return execute_sql_tool_by_connection_id(connection_info['id'], sql)
@mcp.tool()
def find_matching_reports(dbtools_connection_display_name: str, search_text: str, limit: int = 5) -> str:
"""
Find reports similar to the given search text using vector similarity search.
Args:
dbtools_connection_display_name: The name of the database connection
search_text: Text to find similar reports for
limit: Maximum number of similar reports to return (default: 5)
Returns:
JSON string containing the matched reports sorted by similarity score
"""
connection_info = get_minimal_connection_by_name(dbtools_connection_display_name)
if connection_info is None:
return json.dumps({"error": f"No connection found with name '{dbtools_connection_display_name}'"})
# Check if table exists
bootstrap_result = bootstrap_reports(dbtools_connection_display_name)
try:
result = json.loads(bootstrap_result)
if "error" in result:
return bootstrap_result
except:
return bootstrap_result
# Query similar reports using vector similarity
query = f"""
SELECT
r.name as "name",
r.description as "description",
TO_CHAR(r.time_created, 'YYYY-MM-DD"T"HH24:MI:SS"Z"') as "time_created",
TO_CHAR(r.time_updated, 'YYYY-MM-DD"T"HH24:MI:SS"Z"') as "time_updated",
r.sql_definition as "sql_definition",
ROUND(1 - VECTOR_DISTANCE(r.text_vector,
VECTOR_EMBEDDING({MODEL_NAME} USING :search_text AS data)), 4) as "similarity_score"
FROM report_definitions r
WHERE r.text_vector IS NOT NULL
AND 1 - VECTOR_DISTANCE(r.text_vector,
VECTOR_EMBEDDING({MODEL_NAME} USING :search_text AS data)) > 0.3
ORDER BY "similarity_score" DESC
FETCH FIRST :limit ROWS ONLY
"""
binds = [
{"name": "search_text", "data_type": "VARCHAR", "value": search_text},
{"name": "limit", "data_type": "NUMBER", "value": limit}
]
result = execute_sql_tool_by_connection_id(connection_info['id'], query, binds)
try:
# Parse the results
json_result = json.loads(result)
if "error" in json_result:
return json.dumps({
"error": "Failed to find matching reports",
"details": json_result["error"]
})
# Extract the reports from the result
reports = []
if "items" in json_result and json_result["items"] and "resultSet" in json_result["items"][0]:
result_set = json_result["items"][0]["resultSet"]
if "items" in result_set:
for item in result_set["items"]:
# Handle sql_definition which may already be parsed as JSON
sql_def = item["sql_definition"]
if isinstance(sql_def, str):
try:
sql_def = json.loads(sql_def)
except:
pass
reports.append({
"name": item["name"],
"description": item["description"],
"time_created": item["time_created"],
"time_updated": item["time_updated"],
"sql_definition": sql_def,
"similarity_score": float(item["similarity_score"])
})
return json.dumps({
"ok": True,
"reports": reports,
"message": f"Found {len(reports)} similar reports for '{search_text}'"
})