Skip to content

Commit 31bddaf

Browse files
authored
Merge pull request #18 from volcengine/feat/env-route
feat: support env detect routes for a2a and mcp server
2 parents a4a1b4a + cf2488d commit 31bddaf

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

agentkit/apps/a2a_app/a2a_app.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import logging
16+
import os
1617
from typing import Callable, override
1718

1819
import uvicorn
@@ -24,9 +25,12 @@
2425
from a2a.server.tasks import InMemoryTaskStore
2526
from a2a.server.tasks.task_store import TaskStore
2627
from a2a.types import AgentCard
28+
from starlette.applications import Starlette
29+
from starlette.responses import JSONResponse
30+
from starlette.routing import Route
2731

28-
from agentkit.apps.base_app import BaseAgentkitApp
2932
from agentkit.apps.a2a_app.telemetry import telemetry
33+
from agentkit.apps.base_app import BaseAgentkitApp
3034

3135
logger = logging.getLogger(__name__)
3236

@@ -37,7 +41,9 @@ async def wrapper(*args, **kwargs):
3741
context: RequestContext = args[1]
3842
event_queue: EventQueue = args[2]
3943

40-
with telemetry.tracer.start_as_current_span(name="a2a_invocation") as span:
44+
with telemetry.tracer.start_as_current_span(
45+
name="a2a_invocation"
46+
) as span:
4147
exception = None
4248
try:
4349
result = await execute_func(
@@ -80,7 +86,9 @@ def wrapper(cls: type) -> type[AgentExecutor]:
8086
)
8187

8288
if self._agent_executor:
83-
raise RuntimeError("An executor is already bound to this app instance.")
89+
raise RuntimeError(
90+
"An executor is already bound to this app instance."
91+
)
8492

8593
# Wrap the execute method for intercepting context and event_queue
8694
cls.execute = _wrap_agent_executor_execute_func(cls.execute)
@@ -111,6 +119,23 @@ def wrapper(cls: type) -> type[TaskStore]:
111119

112120
return wrapper
113121

122+
def add_env_detect_route(self, app: Starlette):
123+
def is_agentkit_runtime() -> bool:
124+
if os.getenv("RUNTIME_IAM_ROLE_TRN", ""):
125+
return True
126+
else:
127+
return False
128+
129+
route = Route(
130+
"/env",
131+
endpoint=lambda request: JSONResponse(
132+
{"env": "agentkit" if is_agentkit_runtime() else "veadk"}
133+
),
134+
methods=["GET"],
135+
name="env_detect",
136+
)
137+
app.routes.append(route)
138+
114139
@override
115140
def run(self, agent_card: AgentCard, host: str, port: int = 8000):
116141
if not self._agent_executor:
@@ -130,4 +155,6 @@ def run(self, agent_card: AgentCard, host: str, port: int = 8000):
130155
),
131156
).build()
132157

158+
self.add_env_detect_route(a2a_app)
159+
133160
uvicorn.run(a2a_app, host=host, port=port)

agentkit/apps/mcp_app/mcp_app.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import inspect
1717
import logging
18+
import os
1819
from functools import wraps
1920
from typing import Any, Callable, override
2021

@@ -39,7 +40,9 @@ def tool(self, func: Callable) -> Callable:
3940
@wraps(func)
4041
async def async_wrapper(*args, **kwargs) -> Any:
4142
# with tracer.start_as_current_span("tool") as span:
42-
with telemetry.tracer.start_as_current_span(name="tool") as span:
43+
with telemetry.tracer.start_as_current_span(
44+
name="tool"
45+
) as span:
4346
exception = None
4447
try:
4548
result = await func(*args, **kwargs)
@@ -67,7 +70,9 @@ async def async_wrapper(*args, **kwargs) -> Any:
6770
@wraps(func)
6871
def sync_wrapper(*args, **kwargs) -> Any:
6972
# with tracer.start_as_current_span("tool") as span:
70-
with telemetry.tracer.start_as_current_span(name="tool") as span:
73+
with telemetry.tracer.start_as_current_span(
74+
name="tool"
75+
) as span:
7176
exception = None
7277
try:
7378
result = func(*args, **kwargs)
@@ -95,7 +100,9 @@ def agent_as_a_tool(self, func: Callable) -> Callable:
95100

96101
@wraps(func)
97102
async def async_wrapper(*args, **kwargs) -> Any:
98-
with telemetry.tracer.start_as_current_span(name="tool") as span:
103+
with telemetry.tracer.start_as_current_span(
104+
name="tool"
105+
) as span:
99106
exception = None
100107
try:
101108
result = await func(*args, **kwargs)
@@ -119,7 +126,9 @@ async def async_wrapper(*args, **kwargs) -> Any:
119126

120127
@wraps(func)
121128
def sync_wrapper(*args, **kwargs) -> Any:
122-
with telemetry.tracer.start_as_current_span(name="tool") as span:
129+
with telemetry.tracer.start_as_current_span(
130+
name="tool"
131+
) as span:
123132
exception = None
124133
try:
125134
result = func(*args, **kwargs)
@@ -142,8 +151,25 @@ def sync_wrapper(*args, **kwargs) -> Any:
142151

143152
return func
144153

154+
def add_env_detect_tool(self):
155+
def is_agentkit_runtime() -> bool:
156+
if os.getenv("RUNTIME_IAM_ROLE_TRN", ""):
157+
return True
158+
else:
159+
return False
160+
161+
def get_env() -> dict:
162+
return {"env": "agentkit" if is_agentkit_runtime() else "veadk"}
163+
164+
self._mcp_server.tool(get_env)
165+
145166
@override
146167
def run(
147-
self, host: str, port: int = 8000, transport: Transport = "streamable-http"
168+
self,
169+
host: str,
170+
port: int = 8000,
171+
transport: Transport = "streamable-http",
148172
) -> None:
173+
self.add_env_detect_tool()
174+
149175
self._mcp_server.run(host=host, port=port, transport=transport)

0 commit comments

Comments
 (0)