-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgateway.py
More file actions
177 lines (156 loc) · 7.34 KB
/
Copy pathgateway.py
File metadata and controls
177 lines (156 loc) · 7.34 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
import os
import utilities.utils as utils
import boto3
from bedrock_agentcore_starter_toolkit.operations.gateway.client import GatewayClient
import logging
agent_control = boto3.client(service_name="bedrock-agentcore-control")
os.environ['AWS_DEFAULT_REGION'] = os.environ.get('AWS_REGION', 'us-east-1')
REGION = os.environ['AWS_DEFAULT_REGION']
client = GatewayClient(region_name=REGION)
client.logger.setLevel(logging.DEBUG)
def main():
agentcore_gateway_iam_role = utils.create_agentcore_gateway_role("lambda_mcp_agentcore_role")
print("Agentcore gateway role ARN: ", agentcore_gateway_iam_role['Role']['Arn'])
# create cognito authorizer
cognito_response = client.create_oauth_authorizer_with_cognito("LambdaGatewayAuth")
print("cognito response", cognito_response["authorizer_config"])
# create the gateway
gateway = client.create_mcp_gateway(
name='LambdaMCPGATEWAY', # the name of the Gateway - if you don't set one, one will be generated.
role_arn=agentcore_gateway_iam_role['Role']['Arn'], # the role arn that the Gateway will use - if you don't set one, one will be created.
# Variable from inbound authorization setup steps. Contains the OAuth authorizer details for authorizing callers to your Gateway (MCP only supports OAuth).
enable_semantic_search=True, # enable semantic search.
authorizer_config=cognito_response["authorizer_config"])
# Get the gateway ID from the gateway object
gateway_id = gateway.get('gatewayId') or gateway.get('gatewayArn').split('/')[-1]
print(f"Gateway ID: {gateway_id}")
# Get the gateway url
gateway_url = gateway.get('gatewayUrl')
print(f"Gateway URL: {gateway_url}")
lambda_target_config = {
"mcp": {
"lambda": {
"lambdaArn": "arn:aws:lambda:us-east-1:132260253285:function:EcommerceApiStack-ToolsLambdaFunction16F38372-6r8yUcU8F7xo",
# Replace this with your AWS Lambda function ARN
"toolSchema": {
"inlinePayload": [
{
"name": "get_all_products_tool",
"description": "tool to get all ecommerce products",
"inputSchema": {
"type": "object",
"properties": {
"lastEvaluatedKey": {
"type": "string"
}
}
}
},
{
"name": "get_single_product_tool",
"description": "tool to get a single ecommerce product ",
"inputSchema": {
"type": "object",
"properties": {
"productId": {
"type": "string"
}
},
"required": ["productId"]
}
},
{
"name": "add_product_tool",
"description": "tool to add ecommerce product ",
"inputSchema": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"description": {
"type": "string"
},
"price": {
"type": "number"
},
"stock": {
"type": "number"
},
},
"required": ["id", "name", "description", "price", "stock"]
}
},
{
"name": "get_order_tool",
"description": "tool to get the order",
"inputSchema": {
"type": "object",
"properties": {
"orderId": {
"type": "string"
}
},
"required": ["orderId"]
}
},
{
"name": "place_order_tool",
"description": "tool to place an order",
"inputSchema": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"customerName": {
"type": "string"
},
"items": {
"type": "array",
"description": "Order items",
"items": {
"type": "object",
"properties": {
"productId": {
"type": "string",
"description": "product id"
},
"quantity": {
"type": "number",
"description": "quantity"
}
},
"required": ["productId", "quantity"]
}
},
},
"required": ["id", "customerName", "items"]
}
}
]
}
}
}
}
credential_config = [
{
"credentialProviderType": "GATEWAY_IAM_ROLE"
}
]
try:
targetname = 'LambdaEcommerceTool'
response = agent_control.create_gateway_target(
gatewayIdentifier=gateway_id,
name=targetname,
description='Lambda Target using SDK',
targetConfiguration=lambda_target_config,
credentialProviderConfigurations=credential_config)
print(response)
except Exception as e:
print(f'Error uploading file: {e}')
if __name__ == "__main__":
main()