Skip to content

Commit 3d60f9b

Browse files
committed
feat: skip MCP client generation for VPC agents
VPC agents may not have public internet access, so Exa AI MCP server should not be included in generated code. Adds isVpc flag to template rendering and conditionally excludes mcp_client/ directory and MCP imports in main.py for all frameworks.
1 parent c75f4cd commit 3d60f9b

11 files changed

Lines changed: 130 additions & 4 deletions

File tree

src/assets/__tests__/__snapshots__/assets.snapshot.test.ts.snap

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,9 @@ from autogen_agentchat.agents import AssistantAgent
904904
from autogen_core.tools import FunctionTool
905905
from bedrock_agentcore.runtime import BedrockAgentCoreApp
906906
from model.load import load_model
907+
{{#unless isVpc}}
907908
from mcp_client.client import get_streamable_http_mcp_tools
909+
{{/unless}}
908910
909911
app = BedrockAgentCoreApp()
910912
log = app.logger
@@ -928,14 +930,20 @@ tools = [add_numbers_tool]
928930
async def invoke(payload, context):
929931
log.info("Invoking Agent.....")
930932
933+
{{#unless isVpc}}
931934
# Get MCP Tools
932935
mcp_tools = await get_streamable_http_mcp_tools()
933936
937+
{{/unless}}
934938
# Define an AssistantAgent with the model and tools
935939
agent = AssistantAgent(
936940
name="{{ name }}",
937941
model_client=load_model(),
942+
{{#unless isVpc}}
938943
tools=tools + mcp_tools,
944+
{{else}}
945+
tools=tools,
946+
{{/unless}}
939947
system_message="You are a helpful assistant. Use tools when appropriate.",
940948
)
941949
@@ -1584,7 +1592,9 @@ from google.adk.sessions import InMemorySessionService
15841592
from google.genai import types
15851593
from bedrock_agentcore.runtime import BedrockAgentCoreApp
15861594
from model.load import load_model
1595+
{{#unless isVpc}}
15871596
from mcp_client.client import get_streamable_http_mcp_client
1597+
{{/unless}}
15881598
15891599
app = BedrockAgentCoreApp()
15901600
log = app.logger
@@ -1601,8 +1611,10 @@ def add_numbers(a: int, b: int) -> int:
16011611
return a + b
16021612
16031613
1614+
{{#unless isVpc}}
16041615
# Get MCP Toolset
16051616
mcp_toolset = [get_streamable_http_mcp_client()]
1617+
{{/unless}}
16061618
16071619
_credentials_loaded = False
16081620
@@ -1619,7 +1631,11 @@ agent = Agent(
16191631
name="{{ name }}",
16201632
description="Agent to answer questions",
16211633
instruction="I can answer your questions using the knowledge I have!",
1634+
{{#unless isVpc}}
16221635
tools=mcp_toolset + [add_numbers],
1636+
{{else}}
1637+
tools=[add_numbers],
1638+
{{/unless}}
16231639
)
16241640
16251641
@@ -1864,7 +1880,9 @@ from langgraph.prebuilt import create_react_agent
18641880
from langchain.tools import tool
18651881
from bedrock_agentcore.runtime import BedrockAgentCoreApp
18661882
from model.load import load_model
1883+
{{#unless isVpc}}
18671884
from mcp_client.client import get_streamable_http_mcp_client
1885+
{{/unless}}
18681886
18691887
app = BedrockAgentCoreApp()
18701888
log = app.logger
@@ -1893,14 +1911,20 @@ tools = [add_numbers]
18931911
async def invoke(payload, context):
18941912
log.info("Invoking Agent.....")
18951913
1914+
{{#unless isVpc}}
18961915
# Get MCP Client
18971916
mcp_client = get_streamable_http_mcp_client()
18981917
18991918
# Load MCP Tools
19001919
mcp_tools = await mcp_client.get_tools()
19011920
1921+
{{/unless}}
19021922
# Define the agent using create_react_agent
1923+
{{#unless isVpc}}
19031924
graph = create_react_agent(get_or_create_model(), tools=mcp_tools + tools)
1925+
{{else}}
1926+
graph = create_react_agent(get_or_create_model(), tools=tools)
1927+
{{/unless}}
19041928
19051929
# Process the user prompt
19061930
prompt = payload.get("prompt", "What can you help me with?")
@@ -2210,13 +2234,17 @@ exports[`Assets Directory Snapshots > Python framework assets > python/python/op
22102234
from agents import Agent, Runner, function_tool
22112235
from bedrock_agentcore.runtime import BedrockAgentCoreApp
22122236
from model.load import load_model
2237+
{{#unless isVpc}}
22132238
from mcp_client.client import get_streamable_http_mcp_client
2239+
{{/unless}}
22142240
22152241
app = BedrockAgentCoreApp()
22162242
log = app.logger
22172243
2244+
{{#unless isVpc}}
22182245
# Get MCP Server
22192246
mcp_server = get_streamable_http_mcp_client()
2247+
{{/unless}}
22202248
22212249
_credentials_loaded = False
22222250
@@ -2234,6 +2262,7 @@ def add_numbers(a: int, b: int) -> int:
22342262
return a + b
22352263
22362264
2265+
{{#unless isVpc}}
22372266
# Define the agent execution
22382267
async def main(query):
22392268
ensure_credentials_loaded()
@@ -2251,6 +2280,22 @@ async def main(query):
22512280
except Exception as e:
22522281
log.error(f"Error during agent execution: {e}", exc_info=True)
22532282
raise e
2283+
{{else}}
2284+
# Define the agent execution
2285+
async def main(query):
2286+
ensure_credentials_loaded()
2287+
try:
2288+
agent = Agent(
2289+
name="{{ name }}",
2290+
model="gpt-4.1",
2291+
tools=[add_numbers]
2292+
)
2293+
result = await Runner.run(agent, query)
2294+
return result
2295+
except Exception as e:
2296+
log.error(f"Error during agent execution: {e}", exc_info=True)
2297+
raise e
2298+
{{/unless}}
22542299
22552300
22562301
@app.entrypoint
@@ -2455,16 +2500,20 @@ exports[`Assets Directory Snapshots > Python framework assets > python/python/st
24552500
"from strands import Agent, tool
24562501
from bedrock_agentcore.runtime import BedrockAgentCoreApp
24572502
from model.load import load_model
2503+
{{#unless isVpc}}
24582504
from mcp_client.client import get_streamable_http_mcp_client
2505+
{{/unless}}
24592506
{{#if hasMemory}}
24602507
from memory.session import get_memory_session_manager
24612508
{{/if}}
24622509
24632510
app = BedrockAgentCoreApp()
24642511
log = app.logger
24652512
2513+
{{#unless isVpc}}
24662514
# Define a Streamable HTTP MCP Client
24672515
mcp_client = get_streamable_http_mcp_client()
2516+
{{/unless}}
24682517
24692518
# Define a collection of tools used by the model
24702519
tools = []
@@ -2490,7 +2539,11 @@ def agent_factory():
24902539
system_prompt="""
24912540
You are a helpful assistant. Use tools when appropriate.
24922541
""",
2542+
{{#unless isVpc}}
24932543
tools=tools+[mcp_client]
2544+
{{else}}
2545+
tools=tools
2546+
{{/unless}}
24942547
)
24952548
return cache[key]
24962549
return get_or_create_agent
@@ -2506,7 +2559,11 @@ def get_or_create_agent():
25062559
system_prompt="""
25072560
You are a helpful assistant. Use tools when appropriate.
25082561
""",
2562+
{{#unless isVpc}}
25092563
tools=tools+[mcp_client]
2564+
{{else}}
2565+
tools=tools
2566+
{{/unless}}
25102567
)
25112568
return _agent
25122569
{{/if}}

src/assets/python/autogen/base/main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
from autogen_core.tools import FunctionTool
44
from bedrock_agentcore.runtime import BedrockAgentCoreApp
55
from model.load import load_model
6+
{{#unless isVpc}}
67
from mcp_client.client import get_streamable_http_mcp_tools
8+
{{/unless}}
79

810
app = BedrockAgentCoreApp()
911
log = app.logger
@@ -27,14 +29,20 @@ def add_numbers(a: int, b: int) -> int:
2729
async def invoke(payload, context):
2830
log.info("Invoking Agent.....")
2931

32+
{{#unless isVpc}}
3033
# Get MCP Tools
3134
mcp_tools = await get_streamable_http_mcp_tools()
3235

36+
{{/unless}}
3337
# Define an AssistantAgent with the model and tools
3438
agent = AssistantAgent(
3539
name="{{ name }}",
3640
model_client=load_model(),
41+
{{#unless isVpc}}
3742
tools=tools + mcp_tools,
43+
{{else}}
44+
tools=tools,
45+
{{/unless}}
3846
system_message="You are a helpful assistant. Use tools when appropriate.",
3947
)
4048

src/assets/python/googleadk/base/main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
from google.genai import types
66
from bedrock_agentcore.runtime import BedrockAgentCoreApp
77
from model.load import load_model
8+
{{#unless isVpc}}
89
from mcp_client.client import get_streamable_http_mcp_client
10+
{{/unless}}
911

1012
app = BedrockAgentCoreApp()
1113
log = app.logger
@@ -22,8 +24,10 @@ def add_numbers(a: int, b: int) -> int:
2224
return a + b
2325

2426

27+
{{#unless isVpc}}
2528
# Get MCP Toolset
2629
mcp_toolset = [get_streamable_http_mcp_client()]
30+
{{/unless}}
2731

2832
_credentials_loaded = False
2933

@@ -40,7 +44,11 @@ def ensure_credentials_loaded():
4044
name="{{ name }}",
4145
description="Agent to answer questions",
4246
instruction="I can answer your questions using the knowledge I have!",
47+
{{#unless isVpc}}
4348
tools=mcp_toolset + [add_numbers],
49+
{{else}}
50+
tools=[add_numbers],
51+
{{/unless}}
4452
)
4553

4654

src/assets/python/langchain_langgraph/base/main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
from langchain.tools import tool
55
from bedrock_agentcore.runtime import BedrockAgentCoreApp
66
from model.load import load_model
7+
{{#unless isVpc}}
78
from mcp_client.client import get_streamable_http_mcp_client
9+
{{/unless}}
810

911
app = BedrockAgentCoreApp()
1012
log = app.logger
@@ -33,14 +35,20 @@ def add_numbers(a: int, b: int) -> int:
3335
async def invoke(payload, context):
3436
log.info("Invoking Agent.....")
3537

38+
{{#unless isVpc}}
3639
# Get MCP Client
3740
mcp_client = get_streamable_http_mcp_client()
3841

3942
# Load MCP Tools
4043
mcp_tools = await mcp_client.get_tools()
4144

45+
{{/unless}}
4246
# Define the agent using create_react_agent
47+
{{#unless isVpc}}
4348
graph = create_react_agent(get_or_create_model(), tools=mcp_tools + tools)
49+
{{else}}
50+
graph = create_react_agent(get_or_create_model(), tools=tools)
51+
{{/unless}}
4452

4553
# Process the user prompt
4654
prompt = payload.get("prompt", "What can you help me with?")

src/assets/python/openaiagents/base/main.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
from agents import Agent, Runner, function_tool
33
from bedrock_agentcore.runtime import BedrockAgentCoreApp
44
from model.load import load_model
5+
{{#unless isVpc}}
56
from mcp_client.client import get_streamable_http_mcp_client
7+
{{/unless}}
68

79
app = BedrockAgentCoreApp()
810
log = app.logger
911

12+
{{#unless isVpc}}
1013
# Get MCP Server
1114
mcp_server = get_streamable_http_mcp_client()
15+
{{/unless}}
1216

1317
_credentials_loaded = False
1418

@@ -26,6 +30,7 @@ def add_numbers(a: int, b: int) -> int:
2630
return a + b
2731

2832

33+
{{#unless isVpc}}
2934
# Define the agent execution
3035
async def main(query):
3136
ensure_credentials_loaded()
@@ -43,6 +48,22 @@ async def main(query):
4348
except Exception as e:
4449
log.error(f"Error during agent execution: {e}", exc_info=True)
4550
raise e
51+
{{else}}
52+
# Define the agent execution
53+
async def main(query):
54+
ensure_credentials_loaded()
55+
try:
56+
agent = Agent(
57+
name="{{ name }}",
58+
model="gpt-4.1",
59+
tools=[add_numbers]
60+
)
61+
result = await Runner.run(agent, query)
62+
return result
63+
except Exception as e:
64+
log.error(f"Error during agent execution: {e}", exc_info=True)
65+
raise e
66+
{{/unless}}
4667

4768

4869
@app.entrypoint

src/assets/python/strands/base/main.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
from strands import Agent, tool
22
from bedrock_agentcore.runtime import BedrockAgentCoreApp
33
from model.load import load_model
4+
{{#unless isVpc}}
45
from mcp_client.client import get_streamable_http_mcp_client
6+
{{/unless}}
57
{{#if hasMemory}}
68
from memory.session import get_memory_session_manager
79
{{/if}}
810

911
app = BedrockAgentCoreApp()
1012
log = app.logger
1113

14+
{{#unless isVpc}}
1215
# Define a Streamable HTTP MCP Client
1316
mcp_client = get_streamable_http_mcp_client()
17+
{{/unless}}
1418

1519
# Define a collection of tools used by the model
1620
tools = []
@@ -36,7 +40,11 @@ def get_or_create_agent(session_id, user_id):
3640
system_prompt="""
3741
You are a helpful assistant. Use tools when appropriate.
3842
""",
43+
{{#unless isVpc}}
3944
tools=tools+[mcp_client]
45+
{{else}}
46+
tools=tools
47+
{{/unless}}
4048
)
4149
return cache[key]
4250
return get_or_create_agent
@@ -52,7 +60,11 @@ def get_or_create_agent():
5260
system_prompt="""
5361
You are a helpful assistant. Use tools when appropriate.
5462
""",
63+
{{#unless isVpc}}
5564
tools=tools+[mcp_client]
65+
{{else}}
66+
tools=tools
67+
{{/unless}}
5668
)
5769
return _agent
5870
{{/if}}

src/cli/operations/agent/generate/schema-mapper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ export function mapGenerateConfigToRenderConfig(
202202
modelProvider: config.modelProvider,
203203
hasMemory: config.memory !== 'none',
204204
hasIdentity: identityProviders.length > 0,
205+
isVpc: config.networkMode === 'VPC',
205206
buildType: config.buildType,
206207
memoryProviders: mapMemoryOptionToMemoryProviders(config.memory, config.projectName),
207208
identityProviders,

src/cli/templates/BaseRenderer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ export abstract class BaseRenderer {
4545
hasMcp: false, // MCP is configured separately
4646
};
4747

48-
// Always render base template
48+
// Always render base template (skip mcp_client for VPC - no public internet for external MCP servers)
4949
const baseDir = path.join(templateDir, 'base');
50-
await copyAndRenderDir(baseDir, projectDir, templateData);
50+
const skipDirs = this.config.isVpc ? new Set(['mcp_client']) : undefined;
51+
await copyAndRenderDir(baseDir, projectDir, templateData, skipDirs);
5152

5253
// Render capability templates based on config
5354
// Only render if the capability directory exists (not all SDKs have all capabilities)

0 commit comments

Comments
 (0)