forked from aws/mcp-proxy-for-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
118 lines (95 loc) · 4.4 KB
/
main.py
File metadata and controls
118 lines (95 loc) · 4.4 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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Example: Using MCP Proxy for AWS as a client for Microsoft Agent Framework integration.
This example demonstrates how to use the aws_iam_streamablehttp_client with the Microsoft Agent Framework
to connect an AI agent to an MCP server using AWS IAM authentication.
Setup:
======
1. Configure AWS credentials (via AWS CLI, environment variables, or IAM roles)
2. Set the following environment variables (or create a .env file):
- MCP_URL: The URL of your MCP server
- MCP_SERVICE: AWS service hosting the MCP server (e.g., "bedrock-agentcore")
- MCP_REGION: AWS region where the MCP server is hosted (e.g., "us-west-2")
- OPENAI_API_KEY: Your OpenAI API key for the LLM
3. Run: `uv run main.py`
Example .env file:
==================
MCP_SERVER_URL=https://example.gateway.bedrock-agentcore.us-west-2.amazonaws.com/mcp
MCP_SERVER_AWS_SERVICE=bedrock-agentcore
MCP_SERVER_REGION=us-west-2
OPENAI_API_KEY=sk-...
"""
import asyncio
import dotenv
import os
from agent_framework import Agent
from agent_framework._mcp import MCPStreamableHTTPTool
from agent_framework.openai import OpenAIChatClient
from contextlib import asynccontextmanager
from mcp_proxy_for_aws.client import aws_iam_streamablehttp_client
# Load configuration from .env file (if present)
dotenv.load_dotenv()
# MCP server configuration - can be set via environment variables or .env file
try:
MCP_URL = os.environ['MCP_SERVER_URL']
MCP_SERVICE = os.environ['MCP_SERVER_AWS_SERVICE']
MCP_REGION = os.environ['MCP_SERVER_REGION']
except KeyError:
raise AssertionError('Please follow the README to setup environment variables.')
# OpenAI API key for the language model
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY', '<Your OpenAI API Key>')
# The model for the agent (using GPT-4.1 Mini as an example)
OPENAI_MODEL_ID = 'gpt-4.1-mini'
@asynccontextmanager
async def create_agent():
"""Create an Agent Framework agent with AWS IAM-authenticated MCP server access.
This function demonstrates the key integration pattern:
1. Define an aws_iam_streamablehttp_client factory function with the MCP server details
2. Initialize an MCPStreamableHTTPTool and add the client factory
3. Create an agent with access to the tools provided by the MCP server
4. Return a callable interface to communicate with the agent
"""
# Define an MCP client factory function for AWS IAM authentication
def mcp_client_factory():
return aws_iam_streamablehttp_client(
endpoint=MCP_URL, aws_region=MCP_REGION, aws_service=MCP_SERVICE
)
# Create an Agent Framework MCP and add the client
mcp_tools = MCPStreamableHTTPTool(name='MCP Tools', url=MCP_URL)
mcp_tools.get_mcp_client = mcp_client_factory
# Connect to the MCP server and create the agent
async with mcp_tools:
agent = Agent(
client=OpenAIChatClient(model=OPENAI_MODEL_ID, api_key=OPENAI_API_KEY),
tools=[mcp_tools],
)
# Yield a callable interface to the agent
async def agent_callable(user_input: str) -> str:
"""Send a message to the agent and return its response."""
result = await agent.run(user_input)
return str(result)
yield agent_callable
async def main():
"""Run the agent example by asking it to list its available tools."""
# Validate required environment variables
if not MCP_URL or not MCP_REGION or not MCP_SERVICE or not OPENAI_API_KEY:
raise ValueError(
'Please set OPENAI_API_KEY, MCP_SERVER_URL, MCP_SERVER_REGION, and MCP_SERVER_AWS_SERVICE environment variables or create an .env file.'
)
# Create and run the agent
async with create_agent() as agent:
result = await agent('Show me your available tools.')
print(f'\n{result}')
if __name__ == '__main__':
asyncio.run(main())