2025-07-16 14:07:30 -04:00
import boto3
import os
agentcore_client = boto3 . client (
' bedrock-agentcore-control ' ,
region_name = os . getenv ( ' AWS_REGION ' , ' us-west-2 ' ) ,
endpoint_url = os . getenv ( ' ENDPOINT_URL ' )
)
lambda_target_config = {
" mcp " : {
" lambda " : {
" lambdaArn " : os . getenv ( ' LAMBDA_ARN ' ) ,
" toolSchema " : {
" inlinePayload " : [
{
" name " : " explain_query " ,
2025-07-24 19:15:45 -06:00
" description " : " Analyzes and explains the execution plan for a SQL query to help optimize database performance. Provide the database environment (dev/prod) and the SQL query to analyze. Use action_type default value as explain_query. " ,
2025-07-16 14:07:30 -04:00
" inputSchema " : {
" type " : " object " ,
" properties " : {
" environment " : {
" type " : " string "
} ,
" action_type " : {
2025-07-24 19:15:45 -06:00
" type " : " string " ,
" description " : " The type of action to perform. Use ' explain_query ' for this tool. "
2025-07-16 14:07:30 -04:00
} ,
" query " : {
" type " : " string "
}
} ,
" required " : [ " environment " , " action_type " , " query " ]
}
} ,
{
" name " : " extract_ddl " ,
2025-07-24 19:15:45 -06:00
" description " : " Extracts the DDL (Data Definition Language) for a database object. Provide the environment (dev/prod), object_type (table, view, function, etc.), object_name, and object_schema to get the creation script. Use action_type default value as extract_ddl. " ,
2025-07-16 14:07:30 -04:00
" inputSchema " : {
" type " : " object " ,
" properties " : {
" environment " : {
" type " : " string "
} ,
" action_type " : {
2025-07-24 19:15:45 -06:00
" type " : " string " ,
" description " : " The type of action to perform. Use ' extract_ddl ' for this tool. "
2025-07-16 14:07:30 -04:00
} ,
" object_type " : {
" type " : " string "
} ,
" object_name " : {
" type " : " string "
} ,
" object_schema " : {
" type " : " string "
}
} ,
" required " : [ " environment " , " action_type " , " object_type " , " object_name " , " object_schema " ]
}
} ,
{
" name " : " execute_query " ,
2025-07-24 19:15:45 -06:00
" description " : " Executes a read-only SQL query safely and returns the results with performance metrics. Provide the environment (dev/prod) and the SQL query to execute. Use action_type default value as execute_query. " ,
2025-07-16 14:07:30 -04:00
" inputSchema " : {
" type " : " object " ,
" properties " : {
" environment " : {
" type " : " string "
} ,
" action_type " : {
2025-07-24 19:15:45 -06:00
" type " : " string " ,
" description " : " The type of action to perform. Use ' execute_query ' for this tool. "
2025-07-16 14:07:30 -04:00
} ,
" query " : {
" type " : " string "
}
} ,
" required " : [ " environment " , " action_type " , " query " ]
}
}
]
}
}
}
}
credential_config = [
{
" credentialProviderType " : " GATEWAY_IAM_ROLE "
}
]
response = agentcore_client . create_gateway_target (
gatewayIdentifier = os . getenv ( ' GATEWAY_IDENTIFIER ' ) ,
name = os . getenv ( ' TARGET_NAME ' , ' pg-analyze-db-performance ' ) ,
description = os . getenv ( ' TARGET_DESCRIPTION ' , ' PostgreSQL database performance analysis tool with query execution plan analysis, DDL extraction, and safe read-only query execution capabilities ' ) ,
credentialProviderConfigurations = credential_config ,
targetConfiguration = lambda_target_config )
2025-07-24 19:15:45 -06:00
target_id = response [ ' targetId ' ]
print ( f " Target ID: { target_id } " )
2025-07-16 14:07:30 -04:00
2025-07-24 19:15:45 -06:00
# Create target_config.env file
with open ( ' target_config.env ' , ' w ' ) as f :
f . write ( f " TARGET_ID= { target_id } \n " )