generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 67
Add Schema Retrieval to MySQL MCP server #73 #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Skeftical
wants to merge
3
commits into
oracle:main
Choose a base branch
from
Skeftical:73-mysql-mcp-schema-retrieval
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| local_config.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| Copyright (c) 2025 Oracle and/or its affiliates. | ||
|
|
||
| The Universal Permissive License (UPL), Version 1.0 | ||
|
|
||
| Subject to the condition set forth below, permission is hereby granted to any | ||
| person obtaining a copy of this software, associated documentation and/or data | ||
| (collectively the "Software"), free of charge and under any and all copyright | ||
| rights in the Software, and any and all patent rights owned or freely | ||
| licensable by each licensor hereunder covering either (i) the unmodified | ||
| Software as contributed to or provided by such licensor, or (ii) the Larger | ||
| Works (as defined below), to deal in both | ||
|
|
||
| (a) the Software, and | ||
| (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if | ||
| one is included with the Software (each a "Larger Work" to which the Software | ||
| is contributed by such licensors), | ||
|
|
||
| without restriction, including without limitation the rights to copy, create | ||
| derivative works of, display, perform, and distribute the Software and make, | ||
| use, sell, offer for sale, import, export, have made, and have sold the | ||
| Software and the Larger Work(s), and to sublicense the foregoing rights on | ||
| either these or other terms. | ||
|
|
||
| This license is subject to the following condition: | ||
| The above copyright notice and either this complete permission notice or at | ||
| a minimum a reference to the UPL must be included in all copies or | ||
| substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| """ | ||
| Copyright (c) 2025, Oracle and/or its affiliates. | ||
| Licensed under the Universal Permissive License v1.0 as shown at | ||
| https://oss.oracle.com/licenses/upl. | ||
| """ | ||
|
|
||
| MIN_CONTEXT_SIZE = 10 | ||
| DEFAULT_CONTEXT_SIZE = 20 | ||
| MAX_CONTEXT_SIZE = 100 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,17 +12,15 @@ | |
| from fastmcp import FastMCP | ||
| from mysql import connector | ||
| from mysql.connector.abstracts import MySQLConnectionAbstract | ||
| from utils import ( | ||
| from oracle.mysql_mcp_server.utils import ( | ||
| DatabaseConnectionError, | ||
| Mode, | ||
| OciInfo, | ||
| get_ssh_command, | ||
| load_mysql_config, | ||
| ) | ||
|
|
||
| MIN_CONTEXT_SIZE = 10 | ||
| DEFAULT_CONTEXT_SIZE = 20 | ||
| MAX_CONTEXT_SIZE = 100 | ||
| from oracle.mysql_mcp_server.consts import MIN_CONTEXT_SIZE, DEFAULT_CONTEXT_SIZE, MAX_CONTEXT_SIZE | ||
|
|
||
| ############################################################### | ||
| # Start setup | ||
|
|
@@ -828,18 +826,21 @@ def ask_nl_sql(connection_id: str, question: str) -> str: | |
| set_response = _execute_sql_tool(db_connection, "SET @response = NULL;") | ||
| if check_error(set_response): | ||
| return json.dumps({"error": f"Error with NL_SQL: {set_response}"}) | ||
|
|
||
| if db_connection.database is not None: | ||
| call_nl_sql = f"CALL sys.NL_SQL(%s, @response, JSON_OBJECT('schemas', JSON_ARRAY(\"{db_connection.database}\"), 'execute', FALSE, 'model_id', 'meta.llama-4-maverick-17b-128e-instruct-fp8'))" | ||
| else: | ||
| call_nl_sql = "CALL sys.NL_SQL(%s, @response, NULL)" | ||
| nl2sql_response = _execute_sql_tool( | ||
| db_connection, | ||
| f"CALL sys.NL_SQL(%s, @response, NULL)", | ||
| call_nl_sql, | ||
| params=[question], | ||
| ) | ||
| if check_error(nl2sql_response): | ||
| return json.dumps({"error": f"Error with NL_SQL: {nl2sql_response}"}) | ||
|
|
||
| fetch_response = _execute_sql_tool(db_connection, "SELECT @response;") | ||
| if check_error(fetch_response): | ||
| return json.dumps({"error": f"Error with ML_RAG: {fetch_response}"}) | ||
| return json.dumps({"error": f"Error with NL_SQL: {fetch_response}"}) | ||
|
|
||
| try: | ||
| response = json.loads(fetch_one(fetch_response)) | ||
|
|
@@ -849,11 +850,90 @@ def ask_nl_sql(connection_id: str, question: str) -> str: | |
| return json.dumps({"error": "Unexpected response format from NL_SQL"}) | ||
|
|
||
|
|
||
| @mcp.tool() | ||
| def retrieve_relevant_schema_information(connection_id: str, question: str) -> str: | ||
| """ | ||
| [MCP Tool] Retrieve relevant schemas and tables for a given natural language question. | ||
|
|
||
| This tool analyzes the input question and, from the provided list of schema and/or table names, | ||
| identifies only those that are relevant with respect to the question. | ||
| It can optionally consider table and column comments for improved semantic matching. The results contain only the relevant schemas and tables in a JSON object. | ||
|
||
|
|
||
| Args: | ||
| connection_id (str): MySQL connection key. | ||
| input (str): Input question for use with ML_SCHEMA_RETRIEVAL. | ||
|
|
||
| Returns: | ||
| JSON object of the form: | ||
| { | ||
| "tables": ["schema1.tableA", "schema2.tableB", ...], | ||
| "schemas": ["schema1","schema",...], | ||
| "schema_metadata": [ | ||
| { | ||
| "fkey_info": [ | ||
| { | ||
| "fkey_column": "customer_id", | ||
| "fkey_ref_col_name": "customer_id", | ||
| "fkey_ref_tbl_name": "customers" | ||
| } | ||
| ], | ||
| "table_name": "customer_addresses", | ||
| "column_info": [ | ||
| { | ||
| "data_type": "int", | ||
| "column_name": "address_id", | ||
| "column_comment": "" | ||
| }, | ||
| { | ||
| "data_type": "int", | ||
| "column_name": "customer_id", | ||
| "column_comment": "" | ||
| },...] | ||
| "table_schema": "db_anatoly", | ||
| "table_comment": "" | ||
| } | ||
| } | ||
| Where both arrays include only the schemas/tables relevant to the question. | ||
|
|
||
| MCP usage example: | ||
| - name: retrieve_relevant_schema_information | ||
| arguments: { | ||
| "input": "Which tables store customer address data?", | ||
| } | ||
| # Example output: | ||
| # { | ||
| # "tables": ["schema1.customers", "schema2.tableB"], | ||
| "schemas": ["schema1","schema"] | ||
| # } | ||
| """ | ||
| with _get_database_connection_cm(connection_id) as db_connection: | ||
| set_response = _execute_sql_tool(db_connection, "SET @response = NULL;") | ||
| if check_error(set_response): | ||
| return json.dumps({"error": f"Error with ML_RETRIEVE_SCHEMA: {set_response}"}) | ||
|
|
||
| ml_retrieval_response = _execute_sql_tool( | ||
| db_connection, | ||
| f"CALL sys.ML_SQL_SCHEMA_METADATA(%s, NULL, TRUE, @response)", | ||
| params=[question], | ||
| ) | ||
| if check_error(ml_retrieval_response): | ||
| return json.dumps({"error": f"Error with ML_RETRIEVE_SCHEMA: {ml_retrieval_response}"}) | ||
|
|
||
| fetch_response = _execute_sql_tool(db_connection, "SELECT @response;") | ||
| if check_error(fetch_response): | ||
| return json.dumps({"error": f"Error with ML_RETRIEVE_SCHEMA: {fetch_response}"}) | ||
|
|
||
| try: | ||
| response = json.loads(fetch_one(fetch_response)) | ||
| return json.dumps(response) | ||
| except: | ||
| return json.dumps({"error": "Unexpected response format from ML_RETRIEVE_SCHEMA"}) | ||
Skeftical marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| """ | ||
| Object store | ||
| """ | ||
|
|
||
|
|
||
| def verify_compartment_access(compartments): | ||
| access_report = {} | ||
|
|
||
|
|
@@ -1034,7 +1114,9 @@ def object_storage_list_objects(namespace: str, bucket_name: str) -> str: | |
|
|
||
| return str(list_object_response.data.objects) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # Initialize and run the server | ||
| def main(): | ||
| """Run the MCP server with CLI argument support.""" | ||
| mcp.run(transport="stdio") | ||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we put the llama4-maverick into constants, also?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now removed. The SP in the backend automatically identifies the right model to use