@aws/agentcore 0.9.1 → 1.0.0-preview.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/dist/agent-inspector/index.css +1 -1
  2. package/dist/agent-inspector/index.js +90 -72
  3. package/dist/assets/README.md +56 -31
  4. package/dist/assets/__tests__/__snapshots__/assets.snapshot.test.ts.snap +1781 -259
  5. package/dist/assets/__tests__/__snapshots__/dockerfile-render.test.ts.snap +77 -0
  6. package/dist/assets/__tests__/dockerfile-render.test.ts +24 -0
  7. package/dist/assets/agents/AGENTS.md +84 -55
  8. package/dist/assets/cdk/bin/cdk.ts +40 -0
  9. package/dist/assets/cdk/cdk.json +1 -1
  10. package/dist/assets/cdk/lib/cdk-stack.ts +59 -2
  11. package/dist/assets/container/python/Dockerfile +4 -0
  12. package/dist/assets/evaluators/python-lambda/execution-role-policy.json +1 -1
  13. package/dist/assets/harness/invoke.py.template +74 -0
  14. package/dist/assets/python/a2a/googleadk/base/main.py +65 -3
  15. package/dist/assets/python/a2a/langchain_langgraph/base/main.py +64 -1
  16. package/dist/assets/python/a2a/strands/base/main.py +65 -2
  17. package/dist/assets/python/agui/googleadk/base/README.md +30 -0
  18. package/dist/assets/python/agui/googleadk/base/gitignore.template +41 -0
  19. package/dist/assets/python/agui/googleadk/base/main.py +31 -0
  20. package/dist/assets/python/agui/googleadk/base/model/__init__.py +1 -0
  21. package/dist/assets/python/agui/googleadk/base/model/load.py +41 -0
  22. package/dist/assets/python/agui/googleadk/base/pyproject.toml +24 -0
  23. package/dist/assets/python/agui/langchain_langgraph/base/README.md +22 -0
  24. package/dist/assets/python/agui/langchain_langgraph/base/gitignore.template +41 -0
  25. package/dist/assets/python/agui/langchain_langgraph/base/main.py +74 -0
  26. package/dist/assets/python/agui/langchain_langgraph/base/model/__init__.py +1 -0
  27. package/dist/assets/python/agui/langchain_langgraph/base/model/load.py +123 -0
  28. package/dist/assets/python/agui/langchain_langgraph/base/pyproject.toml +30 -0
  29. package/dist/assets/python/agui/strands/base/README.md +22 -0
  30. package/dist/assets/python/agui/strands/base/gitignore.template +41 -0
  31. package/dist/assets/python/agui/strands/base/main.py +43 -0
  32. package/dist/assets/python/agui/strands/base/model/__init__.py +1 -0
  33. package/dist/assets/python/agui/strands/base/model/load.py +123 -0
  34. package/dist/assets/python/agui/strands/base/pyproject.toml +27 -0
  35. package/dist/assets/python/agui/strands/capabilities/memory/__init__.py +1 -0
  36. package/dist/assets/python/agui/strands/capabilities/memory/session.py +38 -0
  37. package/dist/assets/python/http/autogen/base/main.py +61 -1
  38. package/dist/assets/python/http/googleadk/base/main.py +62 -2
  39. package/dist/assets/python/http/langchain_langgraph/base/main.py +61 -1
  40. package/dist/assets/python/http/openaiagents/base/main.py +70 -4
  41. package/dist/assets/python/http/strands/base/main.py +64 -6
  42. package/dist/cli/index.mjs +415 -377
  43. package/dist/lib/constants.d.ts +1 -0
  44. package/dist/lib/constants.d.ts.map +1 -1
  45. package/dist/lib/constants.js +3 -1
  46. package/dist/lib/constants.js.map +1 -1
  47. package/dist/lib/errors/config.d.ts.map +1 -1
  48. package/dist/lib/errors/config.js +5 -2
  49. package/dist/lib/errors/config.js.map +1 -1
  50. package/dist/lib/schemas/io/config-io.d.ts +9 -1
  51. package/dist/lib/schemas/io/config-io.d.ts.map +1 -1
  52. package/dist/lib/schemas/io/config-io.js +14 -0
  53. package/dist/lib/schemas/io/config-io.js.map +1 -1
  54. package/dist/lib/schemas/io/path-resolver.d.ts +12 -0
  55. package/dist/lib/schemas/io/path-resolver.d.ts.map +1 -1
  56. package/dist/lib/schemas/io/path-resolver.js +18 -0
  57. package/dist/lib/schemas/io/path-resolver.js.map +1 -1
  58. package/dist/schema/constants.d.ts +1 -0
  59. package/dist/schema/constants.d.ts.map +1 -1
  60. package/dist/schema/constants.js +8 -1
  61. package/dist/schema/constants.js.map +1 -1
  62. package/dist/schema/schemas/agent-env.d.ts +20 -0
  63. package/dist/schema/schemas/agent-env.d.ts.map +1 -1
  64. package/dist/schema/schemas/agent-env.js +17 -2
  65. package/dist/schema/schemas/agent-env.js.map +1 -1
  66. package/dist/schema/schemas/agentcore-project.d.ts +17 -0
  67. package/dist/schema/schemas/agentcore-project.d.ts.map +1 -1
  68. package/dist/schema/schemas/agentcore-project.js +18 -1
  69. package/dist/schema/schemas/agentcore-project.js.map +1 -1
  70. package/dist/schema/schemas/aws-targets.d.ts +3 -0
  71. package/dist/schema/schemas/aws-targets.d.ts.map +1 -1
  72. package/dist/schema/schemas/aws-targets.js +1 -0
  73. package/dist/schema/schemas/aws-targets.js.map +1 -1
  74. package/dist/schema/schemas/deployed-state.d.ts +50 -0
  75. package/dist/schema/schemas/deployed-state.d.ts.map +1 -1
  76. package/dist/schema/schemas/deployed-state.js +15 -1
  77. package/dist/schema/schemas/deployed-state.js.map +1 -1
  78. package/dist/schema/schemas/primitives/harness.d.ts +287 -0
  79. package/dist/schema/schemas/primitives/harness.d.ts.map +1 -0
  80. package/dist/schema/schemas/primitives/harness.js +237 -0
  81. package/dist/schema/schemas/primitives/harness.js.map +1 -0
  82. package/dist/schema/schemas/primitives/index.d.ts +2 -0
  83. package/dist/schema/schemas/primitives/index.d.ts.map +1 -1
  84. package/dist/schema/schemas/primitives/index.js +14 -1
  85. package/dist/schema/schemas/primitives/index.js.map +1 -1
  86. package/package.json +2 -2
  87. package/scripts/bump-version.ts +7 -80
  88. package/scripts/bundle.mjs +57 -3
  89. package/dist/agent-inspector/index.js.map +0 -1
@@ -0,0 +1,41 @@
1
+ # Environment variables
2
+ .env
3
+
4
+ # Python
5
+ __pycache__/
6
+ *.py[cod]
7
+ *$py.class
8
+ *.so
9
+ .Python
10
+ build/
11
+ develop-eggs/
12
+ dist/
13
+ downloads/
14
+ eggs/
15
+ .eggs/
16
+ lib/
17
+ lib64/
18
+ parts/
19
+ sdist/
20
+ var/
21
+ wheels/
22
+ *.egg-info/
23
+ .installed.cfg
24
+ *.egg
25
+
26
+ # Virtual environments
27
+ .venv/
28
+ venv/
29
+ ENV/
30
+ env/
31
+
32
+ # IDE
33
+ .vscode/
34
+ .idea/
35
+ *.swp
36
+ *.swo
37
+ *~
38
+
39
+ # OS
40
+ .DS_Store
41
+ Thumbs.db
@@ -0,0 +1,43 @@
1
+ import os
2
+
3
+ # Suppress OpenTelemetry warnings during local development; remove for production
4
+ if os.getenv("LOCAL_DEV") == "1":
5
+ os.environ["OTEL_SDK_DISABLED"] = "true"
6
+
7
+ import uvicorn
8
+ from strands import Agent, tool
9
+ from ag_ui_strands import StrandsAgent, StrandsAgentConfig, create_strands_app
10
+ from model.load import load_model
11
+ {{#if hasMemory}}
12
+ from memory.session import get_memory_session_manager
13
+ {{/if}}
14
+
15
+
16
+ @tool
17
+ def add_numbers(a: int, b: int) -> int:
18
+ """Return the sum of two numbers."""
19
+ return a + b
20
+
21
+
22
+ tools = [add_numbers]
23
+
24
+ agent = Agent(
25
+ model=load_model(),
26
+ system_prompt="You are a helpful assistant. Use tools when appropriate.",
27
+ tools=tools,
28
+ )
29
+
30
+ {{#if hasMemory}}
31
+ def session_manager_provider(input_data):
32
+ return get_memory_session_manager(input_data.thread_id, "default-user")
33
+
34
+ config = StrandsAgentConfig(session_manager_provider=session_manager_provider)
35
+ {{else}}
36
+ config = StrandsAgentConfig()
37
+ {{/if}}
38
+
39
+ agui_agent = StrandsAgent(agent=agent, name="{{ name }}", description="A helpful assistant", config=config)
40
+ app = create_strands_app(agui_agent, path="/invocations", ping_path="/ping")
41
+
42
+ if __name__ == "__main__":
43
+ uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", "8080")))
@@ -0,0 +1 @@
1
+ # Package marker
@@ -0,0 +1,123 @@
1
+ {{#if (eq modelProvider "Bedrock")}}
2
+ from strands.models.bedrock import BedrockModel
3
+
4
+
5
+ def load_model() -> BedrockModel:
6
+ """Get Bedrock model client using IAM credentials."""
7
+ return BedrockModel(model_id="global.anthropic.claude-sonnet-4-5-20250929-v1:0")
8
+ {{/if}}
9
+ {{#if (eq modelProvider "Anthropic")}}
10
+ import os
11
+
12
+ from strands.models.anthropic import AnthropicModel
13
+ from bedrock_agentcore.identity.auth import requires_api_key
14
+
15
+ IDENTITY_PROVIDER_NAME = "{{identityProviders.[0].name}}"
16
+ IDENTITY_ENV_VAR = "{{identityProviders.[0].envVarName}}"
17
+
18
+
19
+ @requires_api_key(provider_name=IDENTITY_PROVIDER_NAME)
20
+ def _agentcore_identity_api_key_provider(api_key: str) -> str:
21
+ """Fetch API key from AgentCore Identity."""
22
+ return api_key
23
+
24
+
25
+ def _get_api_key() -> str:
26
+ """
27
+ Uses AgentCore Identity for API key management in deployed environments.
28
+ For local development, run via 'agentcore dev' which loads agentcore/.env.
29
+ """
30
+ if os.getenv("LOCAL_DEV") == "1":
31
+ api_key = os.getenv(IDENTITY_ENV_VAR)
32
+ if not api_key:
33
+ raise RuntimeError(
34
+ f"{IDENTITY_ENV_VAR} not found. Add {IDENTITY_ENV_VAR}=your-key to .env.local"
35
+ )
36
+ return api_key
37
+ return _agentcore_identity_api_key_provider()
38
+
39
+
40
+ def load_model() -> AnthropicModel:
41
+ """Get authenticated Anthropic model client."""
42
+ return AnthropicModel(
43
+ client_args={"api_key": _get_api_key()},
44
+ model_id="claude-sonnet-4-5-20250929",
45
+ max_tokens=5000,
46
+ )
47
+ {{/if}}
48
+ {{#if (eq modelProvider "OpenAI")}}
49
+ import os
50
+
51
+ from strands.models.openai import OpenAIModel
52
+ from bedrock_agentcore.identity.auth import requires_api_key
53
+
54
+ IDENTITY_PROVIDER_NAME = "{{identityProviders.[0].name}}"
55
+ IDENTITY_ENV_VAR = "{{identityProviders.[0].envVarName}}"
56
+
57
+
58
+ @requires_api_key(provider_name=IDENTITY_PROVIDER_NAME)
59
+ def _agentcore_identity_api_key_provider(api_key: str) -> str:
60
+ """Fetch API key from AgentCore Identity."""
61
+ return api_key
62
+
63
+
64
+ def _get_api_key() -> str:
65
+ """
66
+ Uses AgentCore Identity for API key management in deployed environments.
67
+ For local development, run via 'agentcore dev' which loads agentcore/.env.
68
+ """
69
+ if os.getenv("LOCAL_DEV") == "1":
70
+ api_key = os.getenv(IDENTITY_ENV_VAR)
71
+ if not api_key:
72
+ raise RuntimeError(
73
+ f"{IDENTITY_ENV_VAR} not found. Add {IDENTITY_ENV_VAR}=your-key to .env.local"
74
+ )
75
+ return api_key
76
+ return _agentcore_identity_api_key_provider()
77
+
78
+
79
+ def load_model() -> OpenAIModel:
80
+ """Get authenticated OpenAI model client."""
81
+ return OpenAIModel(
82
+ client_args={"api_key": _get_api_key()},
83
+ model_id="gpt-4.1",
84
+ )
85
+ {{/if}}
86
+ {{#if (eq modelProvider "Gemini")}}
87
+ import os
88
+
89
+ from strands.models.gemini import GeminiModel
90
+ from bedrock_agentcore.identity.auth import requires_api_key
91
+
92
+ IDENTITY_PROVIDER_NAME = "{{identityProviders.[0].name}}"
93
+ IDENTITY_ENV_VAR = "{{identityProviders.[0].envVarName}}"
94
+
95
+
96
+ @requires_api_key(provider_name=IDENTITY_PROVIDER_NAME)
97
+ def _agentcore_identity_api_key_provider(api_key: str) -> str:
98
+ """Fetch API key from AgentCore Identity."""
99
+ return api_key
100
+
101
+
102
+ def _get_api_key() -> str:
103
+ """
104
+ Uses AgentCore Identity for API key management in deployed environments.
105
+ For local development, run via 'agentcore dev' which loads agentcore/.env.
106
+ """
107
+ if os.getenv("LOCAL_DEV") == "1":
108
+ api_key = os.getenv(IDENTITY_ENV_VAR)
109
+ if not api_key:
110
+ raise RuntimeError(
111
+ f"{IDENTITY_ENV_VAR} not found. Add {IDENTITY_ENV_VAR}=your-key to .env.local"
112
+ )
113
+ return api_key
114
+ return _agentcore_identity_api_key_provider()
115
+
116
+
117
+ def load_model() -> GeminiModel:
118
+ """Get authenticated Gemini model client."""
119
+ return GeminiModel(
120
+ client_args={"api_key": _get_api_key()},
121
+ model_id="gemini-2.5-flash",
122
+ )
123
+ {{/if}}
@@ -0,0 +1,27 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "{{ name }}"
7
+ version = "0.1.0"
8
+ description = "AgentCore AG-UI Agent using Strands SDK"
9
+ readme = "README.md"
10
+ requires-python = ">=3.12"
11
+ dependencies = [
12
+ {{#if (eq modelProvider "Anthropic")}}"anthropic >= 0.30.0",
13
+ {{/if}}"ag-ui-strands >= 0.1.7",
14
+ "ag-ui-protocol >= 0.1.10",
15
+ "aws-opentelemetry-distro",
16
+ "bedrock-agentcore >= 1.0.3",
17
+ "botocore[crt] >= 1.35.0",
18
+ "fastapi >= 0.115.12",
19
+ {{#if (eq modelProvider "Gemini")}}"google-genai >= 1.0.0",
20
+ {{/if}}{{#if (eq modelProvider "OpenAI")}}"openai >= 1.0.0",
21
+ {{/if}}"strands-agents >= 1.15.0",
22
+ "strands-agents-tools >= 0.2.14",
23
+ "uvicorn >= 0.34.3",
24
+ ]
25
+
26
+ [tool.hatch.build.targets.wheel]
27
+ packages = ["."]
@@ -0,0 +1,38 @@
1
+ import os
2
+ from typing import Optional
3
+
4
+ from bedrock_agentcore.memory.integrations.strands.config import AgentCoreMemoryConfig{{#if memoryProviders.[0].strategies.length}}, RetrievalConfig{{/if}}
5
+ from bedrock_agentcore.memory.integrations.strands.session_manager import AgentCoreMemorySessionManager
6
+
7
+ MEMORY_ID = os.getenv("{{memoryProviders.[0].envVarName}}")
8
+ REGION = os.getenv("AWS_REGION")
9
+
10
+ def get_memory_session_manager(session_id: str, actor_id: str) -> Optional[AgentCoreMemorySessionManager]:
11
+ if not MEMORY_ID:
12
+ return None
13
+
14
+ {{#if memoryProviders.[0].strategies.length}}
15
+ retrieval_config = {
16
+ {{#if (includes memoryProviders.[0].strategies "SEMANTIC")}}
17
+ f"/users/{actor_id}/facts": RetrievalConfig(top_k=3, relevance_score=0.5),
18
+ {{/if}}
19
+ {{#if (includes memoryProviders.[0].strategies "USER_PREFERENCE")}}
20
+ f"/users/{actor_id}/preferences": RetrievalConfig(top_k=3, relevance_score=0.5),
21
+ {{/if}}
22
+ {{#if (includes memoryProviders.[0].strategies "SUMMARIZATION")}}
23
+ f"/summaries/{actor_id}/{session_id}": RetrievalConfig(top_k=3, relevance_score=0.5),
24
+ {{/if}}
25
+ }
26
+ {{/if}}
27
+
28
+ return AgentCoreMemorySessionManager(
29
+ AgentCoreMemoryConfig(
30
+ memory_id=MEMORY_ID,
31
+ session_id=session_id,
32
+ actor_id=actor_id,
33
+ {{#if memoryProviders.[0].strategies.length}}
34
+ retrieval_config=retrieval_config,
35
+ {{/if}}
36
+ ),
37
+ REGION
38
+ )
@@ -22,6 +22,66 @@ add_numbers_tool = FunctionTool(
22
22
  # Define a collection of tools used by the model
23
23
  tools = [add_numbers_tool]
24
24
 
25
+ {{#if sessionStorageMountPath}}
26
+ SESSION_STORAGE_PATH = "{{sessionStorageMountPath}}"
27
+
28
+ def _safe_resolve(path: str) -> str:
29
+ """Resolve path safely within the storage boundary."""
30
+ resolved = os.path.realpath(os.path.join(SESSION_STORAGE_PATH, path.lstrip("/")))
31
+ if not resolved.startswith(os.path.realpath(SESSION_STORAGE_PATH)):
32
+ raise ValueError(f"Path '{path}' is outside the storage boundary")
33
+ return resolved
34
+
35
+ def file_read(path: str) -> str:
36
+ """Read a file from persistent storage. The path is relative to the storage root."""
37
+ try:
38
+ full_path = _safe_resolve(path)
39
+ with open(full_path) as f:
40
+ return f.read()
41
+ except ValueError as e:
42
+ return str(e)
43
+ except OSError as e:
44
+ return f"Error reading '{path}': {e.strerror}"
45
+
46
+ def file_write(path: str, content: str) -> str:
47
+ """Write content to a file in persistent storage. The path is relative to the storage root."""
48
+ try:
49
+ full_path = _safe_resolve(path)
50
+ parent = os.path.dirname(full_path)
51
+ if parent:
52
+ os.makedirs(parent, exist_ok=True)
53
+ with open(full_path, "w") as f:
54
+ f.write(content)
55
+ return f"Written to {path}"
56
+ except ValueError as e:
57
+ return str(e)
58
+ except OSError as e:
59
+ return f"Error writing '{path}': {e.strerror}"
60
+
61
+ def list_files(directory: str = "") -> str:
62
+ """List files in persistent storage. The directory is relative to the storage root."""
63
+ try:
64
+ target = _safe_resolve(directory)
65
+ entries = os.listdir(target)
66
+ return "\n".join(entries) if entries else "(empty directory)"
67
+ except ValueError as e:
68
+ return str(e)
69
+ except OSError as e:
70
+ return f"Error listing '{directory}': {e.strerror}"
71
+
72
+ tools.extend([
73
+ FunctionTool(file_read, description="Read a file from persistent storage. The path is relative to the storage root."),
74
+ FunctionTool(file_write, description="Write content to a file in persistent storage. The path is relative to the storage root."),
75
+ FunctionTool(list_files, description="List files in persistent storage. The directory is relative to the storage root."),
76
+ ])
77
+ {{/if}}
78
+
79
+ SYSTEM_MESSAGE = """
80
+ You are a helpful assistant. Use tools when appropriate.
81
+ {{#if sessionStorageMountPath}}
82
+ You have persistent storage at {{sessionStorageMountPath}}. Use file tools to read and write files. Data persists across sessions.
83
+ {{/if}}
84
+ """
25
85
 
26
86
  @app.entrypoint
27
87
  async def invoke(payload, context):
@@ -35,7 +95,7 @@ async def invoke(payload, context):
35
95
  name="{{ name }}",
36
96
  model_client=load_model(),
37
97
  tools=tools + mcp_tools,
38
- system_message="You are a helpful assistant. Use tools when appropriate.",
98
+ system_message=SYSTEM_MESSAGE,
39
99
  )
40
100
 
41
101
  # Process the user prompt
@@ -26,6 +26,66 @@ def add_numbers(a: int, b: int) -> int:
26
26
  return a + b
27
27
 
28
28
 
29
+ # Define a collection of tools used by the model
30
+ tools = [add_numbers]
31
+
32
+ {{#if sessionStorageMountPath}}
33
+ SESSION_STORAGE_PATH = "{{sessionStorageMountPath}}"
34
+
35
+ def _safe_resolve(path: str) -> str:
36
+ """Resolve path safely within the storage boundary."""
37
+ resolved = os.path.realpath(os.path.join(SESSION_STORAGE_PATH, path.lstrip("/")))
38
+ if not resolved.startswith(os.path.realpath(SESSION_STORAGE_PATH)):
39
+ raise ValueError(f"Path '{path}' is outside the storage boundary")
40
+ return resolved
41
+
42
+ def file_read(path: str) -> str:
43
+ """Read a file from persistent storage. The path is relative to the storage root."""
44
+ try:
45
+ full_path = _safe_resolve(path)
46
+ with open(full_path) as f:
47
+ return f.read()
48
+ except ValueError as e:
49
+ return str(e)
50
+ except OSError as e:
51
+ return f"Error reading '{path}': {e.strerror}"
52
+
53
+ def file_write(path: str, content: str) -> str:
54
+ """Write content to a file in persistent storage. The path is relative to the storage root."""
55
+ try:
56
+ full_path = _safe_resolve(path)
57
+ parent = os.path.dirname(full_path)
58
+ if parent:
59
+ os.makedirs(parent, exist_ok=True)
60
+ with open(full_path, "w") as f:
61
+ f.write(content)
62
+ return f"Written to {path}"
63
+ except ValueError as e:
64
+ return str(e)
65
+ except OSError as e:
66
+ return f"Error writing '{path}': {e.strerror}"
67
+
68
+ def list_files(directory: str = "") -> str:
69
+ """List files in persistent storage. The directory is relative to the storage root."""
70
+ try:
71
+ target = _safe_resolve(directory)
72
+ entries = os.listdir(target)
73
+ return "\n".join(entries) if entries else "(empty directory)"
74
+ except ValueError as e:
75
+ return str(e)
76
+ except OSError as e:
77
+ return f"Error listing '{directory}': {e.strerror}"
78
+
79
+ tools.extend([file_read, file_write, list_files])
80
+ {{/if}}
81
+
82
+ AGENT_INSTRUCTION = """
83
+ I can answer your questions using the knowledge I have!
84
+ {{#if sessionStorageMountPath}}
85
+ You have persistent storage at {{sessionStorageMountPath}}. Use file tools to read and write files. Data persists across sessions.
86
+ {{/if}}
87
+ """
88
+
29
89
  # Get MCP Toolset
30
90
  {{#if hasGateway}}
31
91
  mcp_toolset = get_all_gateway_mcp_toolsets()
@@ -48,8 +108,8 @@ agent = Agent(
48
108
  model=MODEL_ID,
49
109
  name="{{ name }}",
50
110
  description="Agent to answer questions",
51
- instruction="I can answer your questions using the knowledge I have!",
52
- tools=mcp_toolset + [add_numbers],
111
+ instruction=AGENT_INSTRUCTION,
112
+ tools=mcp_toolset + tools,
53
113
  )
54
114
 
55
115
 
@@ -35,6 +35,66 @@ def add_numbers(a: int, b: int) -> int:
35
35
  # Define a collection of tools used by the model
36
36
  tools = [add_numbers]
37
37
 
38
+ {{#if sessionStorageMountPath}}
39
+ SESSION_STORAGE_PATH = "{{sessionStorageMountPath}}"
40
+
41
+ def _safe_resolve(path: str) -> str:
42
+ """Resolve path safely within the storage boundary."""
43
+ resolved = os.path.realpath(os.path.join(SESSION_STORAGE_PATH, path.lstrip("/")))
44
+ if not resolved.startswith(os.path.realpath(SESSION_STORAGE_PATH)):
45
+ raise ValueError(f"Path '{path}' is outside the storage boundary")
46
+ return resolved
47
+
48
+ @tool
49
+ def file_read(path: str) -> str:
50
+ """Read a file from persistent storage. The path is relative to the storage root."""
51
+ try:
52
+ full_path = _safe_resolve(path)
53
+ with open(full_path) as f:
54
+ return f.read()
55
+ except ValueError as e:
56
+ return str(e)
57
+ except OSError as e:
58
+ return f"Error reading '{path}': {e.strerror}"
59
+
60
+ @tool
61
+ def file_write(path: str, content: str) -> str:
62
+ """Write content to a file in persistent storage. The path is relative to the storage root."""
63
+ try:
64
+ full_path = _safe_resolve(path)
65
+ parent = os.path.dirname(full_path)
66
+ if parent:
67
+ os.makedirs(parent, exist_ok=True)
68
+ with open(full_path, "w") as f:
69
+ f.write(content)
70
+ return f"Written to {path}"
71
+ except ValueError as e:
72
+ return str(e)
73
+ except OSError as e:
74
+ return f"Error writing '{path}': {e.strerror}"
75
+
76
+ @tool
77
+ def list_files(directory: str = "") -> str:
78
+ """List files in persistent storage. The directory is relative to the storage root."""
79
+ try:
80
+ target = _safe_resolve(directory)
81
+ entries = os.listdir(target)
82
+ return "\n".join(entries) if entries else "(empty directory)"
83
+ except ValueError as e:
84
+ return str(e)
85
+ except OSError as e:
86
+ return f"Error listing '{directory}': {e.strerror}"
87
+
88
+ tools.extend([file_read, file_write, list_files])
89
+ {{/if}}
90
+
91
+ SYSTEM_PROMPT = """
92
+ You are a helpful assistant. Use tools when appropriate.
93
+ {{#if sessionStorageMountPath}}
94
+ You have persistent storage at {{sessionStorageMountPath}}. Use file tools to read and write files. Data persists across sessions.
95
+ {{/if}}
96
+ """
97
+
38
98
 
39
99
  @app.entrypoint
40
100
  async def invoke(payload, context):
@@ -53,7 +113,7 @@ async def invoke(payload, context):
53
113
  mcp_tools = await mcp_client.get_tools()
54
114
 
55
115
  # Define the agent using create_react_agent
56
- graph = create_react_agent(get_or_create_model(), tools=mcp_tools + tools)
116
+ graph = create_react_agent(get_or_create_model(), tools=mcp_tools + tools, prompt=SYSTEM_PROMPT)
57
117
 
58
118
  # Process the user prompt
59
119
  prompt = payload.get("prompt", "What can you help me with?")
@@ -35,6 +35,68 @@ def add_numbers(a: int, b: int) -> int:
35
35
  return a + b
36
36
 
37
37
 
38
+ tools = [add_numbers]
39
+
40
+ {{#if sessionStorageMountPath}}
41
+ SESSION_STORAGE_PATH = "{{sessionStorageMountPath}}"
42
+
43
+ def _safe_resolve(path: str) -> str:
44
+ """Resolve path safely within the storage boundary."""
45
+ resolved = os.path.realpath(os.path.join(SESSION_STORAGE_PATH, path.lstrip("/")))
46
+ if not resolved.startswith(os.path.realpath(SESSION_STORAGE_PATH)):
47
+ raise ValueError(f"Path '{path}' is outside the storage boundary")
48
+ return resolved
49
+
50
+ @function_tool
51
+ def file_read(path: str) -> str:
52
+ """Read a file from persistent storage. The path is relative to the storage root."""
53
+ try:
54
+ full_path = _safe_resolve(path)
55
+ with open(full_path) as f:
56
+ return f.read()
57
+ except ValueError as e:
58
+ return str(e)
59
+ except OSError as e:
60
+ return f"Error reading '{path}': {e.strerror}"
61
+
62
+ @function_tool
63
+ def file_write(path: str, content: str) -> str:
64
+ """Write content to a file in persistent storage. The path is relative to the storage root."""
65
+ try:
66
+ full_path = _safe_resolve(path)
67
+ parent = os.path.dirname(full_path)
68
+ if parent:
69
+ os.makedirs(parent, exist_ok=True)
70
+ with open(full_path, "w") as f:
71
+ f.write(content)
72
+ return f"Written to {path}"
73
+ except ValueError as e:
74
+ return str(e)
75
+ except OSError as e:
76
+ return f"Error writing '{path}': {e.strerror}"
77
+
78
+ @function_tool
79
+ def list_files(directory: str = "") -> str:
80
+ """List files in persistent storage. The directory is relative to the storage root."""
81
+ try:
82
+ target = _safe_resolve(directory)
83
+ entries = os.listdir(target)
84
+ return "\n".join(entries) if entries else "(empty directory)"
85
+ except ValueError as e:
86
+ return str(e)
87
+ except OSError as e:
88
+ return f"Error listing '{directory}': {e.strerror}"
89
+
90
+ tools.extend([file_read, file_write, list_files])
91
+ {{/if}}
92
+
93
+ INSTRUCTIONS = """
94
+ You are a helpful assistant. Use tools when appropriate.
95
+ {{#if sessionStorageMountPath}}
96
+ You have persistent storage at {{sessionStorageMountPath}}. Use file tools to read and write files. Data persists across sessions.
97
+ {{/if}}
98
+ """
99
+
38
100
  # Define the agent execution
39
101
  async def main(query):
40
102
  ensure_credentials_loaded()
@@ -44,8 +106,9 @@ async def main(query):
44
106
  agent = Agent(
45
107
  name="{{ name }}",
46
108
  model="gpt-4.1",
109
+ instructions=INSTRUCTIONS,
47
110
  mcp_servers=mcp_servers,
48
- tools=[add_numbers]
111
+ tools=tools
49
112
  )
50
113
  result = await Runner.run(agent, query)
51
114
  return result
@@ -53,8 +116,9 @@ async def main(query):
53
116
  agent = Agent(
54
117
  name="{{ name }}",
55
118
  model="gpt-4.1",
119
+ instructions=INSTRUCTIONS,
56
120
  mcp_servers=[],
57
- tools=[add_numbers]
121
+ tools=tools
58
122
  )
59
123
  result = await Runner.run(agent, query)
60
124
  return result
@@ -65,8 +129,9 @@ async def main(query):
65
129
  agent = Agent(
66
130
  name="{{ name }}",
67
131
  model="gpt-4.1",
132
+ instructions=INSTRUCTIONS,
68
133
  mcp_servers=active_servers,
69
- tools=[add_numbers]
134
+ tools=tools
70
135
  )
71
136
  result = await Runner.run(agent, query)
72
137
  return result
@@ -74,8 +139,9 @@ async def main(query):
74
139
  agent = Agent(
75
140
  name="{{ name }}",
76
141
  model="gpt-4.1",
142
+ instructions=INSTRUCTIONS,
77
143
  mcp_servers=[],
78
- tools=[add_numbers]
144
+ tools=tools
79
145
  )
80
146
  result = await Runner.run(agent, query)
81
147
  return result