forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (49 loc) · 1.54 KB
/
main.py
File metadata and controls
66 lines (49 loc) · 1.54 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
import os
from langchain_core.messages import HumanMessage
from langgraph.prebuilt import create_react_agent
from langchain.tools import tool
from bedrock_agentcore.runtime import BedrockAgentCoreApp
from model.load import load_model
{{#if hasGateway}}
from mcp_client.client import get_all_gateway_mcp_client
{{else}}
from mcp_client.client import get_streamable_http_mcp_client
{{/if}}
app = BedrockAgentCoreApp()
log = app.logger
_llm = None
def get_or_create_model():
global _llm
if _llm is None:
_llm = load_model()
return _llm
# Define a simple function tool
@tool
def add_numbers(a: int, b: int) -> int:
"""Return the sum of two numbers"""
return a + b
# Define a collection of tools used by the model
tools = [add_numbers]
@app.entrypoint
async def invoke(payload, context):
log.info("Invoking Agent.....")
# Get MCP Client
{{#if hasGateway}}
mcp_client = get_all_gateway_mcp_client()
{{else}}
mcp_client = get_streamable_http_mcp_client()
{{/if}}
# Load MCP Tools
mcp_tools = []
if mcp_client:
mcp_tools = await mcp_client.get_tools()
# Define the agent using create_react_agent
graph = create_react_agent(get_or_create_model(), tools=mcp_tools + tools)
# Process the user prompt
prompt = payload.get("prompt", "What can you help me with?")
# Run the agent
result = await graph.ainvoke({"messages": [HumanMessage(content=prompt)]})
# Return result
return {"result": result["messages"][-1].content}
if __name__ == "__main__":
app.run()