From af3afebd77e82a0993db862dac4570b40fbd7048 Mon Sep 17 00:00:00 2001
From: moonyue-w <300878504+moonyue-w@users.noreply.github.com>
Date: Wed, 4 Nov 2026 20:16:59 +0800
Subject: [PATCH] feat: support BYOC tunnel bindings for MCP requests
---
CHANGELOG.md | 2 +
docs/api/reference.md | 38 ++++++++++++
src/qca/forward/resources/templates.py | 8 +++
src/qca/forward/types/template.py | 1 +
.../forward/types/template_create_params.py | 1 +
.../forward/types/template_update_params.py | 1 +
src/qca/managed/resources/deployments.py | 8 +++
.../managed/resources/sessions/sessions.py | 4 ++
src/qca/managed/types/deployment.py | 1 +
.../managed/types/deployment_create_params.py | 1 +
.../managed/types/deployment_update_params.py | 1 +
.../managed/types/session_create_params.py | 1 +
tests/test_byoc_tunnel.py | 62 +++++++++++++++++++
13 files changed, 129 insertions(+)
create mode 100644 tests/test_byoc_tunnel.py
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 551581a..f0ade20 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,8 @@ existing `0.1.0` release; earlier development prereleases are not listed.
### Added
+- Bind a Managed Session to a BYOC MCP tunnel with `tunnel_id` at creation. On Forward, set the tunnel on a Template for its sessions to inherit. Managed Deployments also accept and return the binding. Template and Deployment updates can replace or clear it.
+
- Add typed Forward Session budgets across create/update responses and events, including omission, explicit null clearing and decimal-string limits.
- Add Forward Session `config.model` overrides and model `speed` configuration, plus optional approval evaluation details, Outcome/status event fields and cumulative top-level Credits on `session.usage`.
- Add `startup_script` to Forward/Managed Environment models and Managed cloud/self-hosted parameters. It runs on sandbox start/resume; the external worker controls self-hosted execution.
diff --git a/docs/api/reference.md b/docs/api/reference.md
index 61ea7a8..8954437 100644
--- a/docs/api/reference.md
+++ b/docs/api/reference.md
@@ -9775,6 +9775,7 @@ def create(
name: str,
model: Union[str, ModelConfigParam],
environment_id: str,
+ tunnel_id: Union[str, NotGiven] = NOT_GIVEN,
description: Union[str, None, NotGiven] = NOT_GIVEN,
system: Union[str, None, NotGiven] = NOT_GIVEN,
tools: Union[List[ToolParam], None, NotGiven] = NOT_GIVEN,
@@ -9845,6 +9846,7 @@ def update(
managed_tool_config: Union[ManagedToolConfigParam, None,
NotGiven] = NOT_GIVEN,
environment_id: Union[str, None, NotGiven] = NOT_GIVEN,
+ tunnel_id: Union[str, None, NotGiven] = NOT_GIVEN,
vaults: Union[Dict[str, ResourceBindingParam], None,
NotGiven] = NOT_GIVEN,
files: Union[Dict[str, ResourceBindingParam], None,
@@ -9951,6 +9953,7 @@ async def create(
name: str,
model: Union[str, ModelConfigParam],
environment_id: str,
+ tunnel_id: Union[str, NotGiven] = NOT_GIVEN,
description: Union[str, None, NotGiven] = NOT_GIVEN,
system: Union[str, None, NotGiven] = NOT_GIVEN,
tools: Union[List[ToolParam], None, NotGiven] = NOT_GIVEN,
@@ -10021,6 +10024,7 @@ async def update(
managed_tool_config: Union[ManagedToolConfigParam, None,
NotGiven] = NOT_GIVEN,
environment_id: Union[str, None, NotGiven] = NOT_GIVEN,
+ tunnel_id: Union[str, None, NotGiven] = NOT_GIVEN,
vaults: Union[Dict[str, ResourceBindingParam], None,
NotGiven] = NOT_GIVEN,
files: Union[Dict[str, ResourceBindingParam], None,
@@ -19174,6 +19178,10 @@ class Template(BaseModel)
#### environment\_id
+
+
+#### tunnel\_id
+
#### vaults
@@ -19288,6 +19296,10 @@ class TemplateCreateParams(TypedDict)
#### environment\_id
+
+
+#### tunnel\_id
+
#### description
@@ -19428,6 +19440,10 @@ class TemplateUpdateParams(TypedDict)
#### environment\_id
+
+
+#### tunnel\_id
+
#### vaults
@@ -21483,6 +21499,7 @@ def create(
*,
agent: Union[str, AgentParams],
environment_id: str,
+ tunnel_id: Union[str, NotGiven] = NOT_GIVEN,
initial_events: List[Union[UserMessageEventParams,
UserDefineOutcomeEventParams,
SystemMessageEventParams]],
@@ -21548,6 +21565,7 @@ def update(
environment_variables: Union[str, None, NotGiven] = NOT_GIVEN,
description: Union[str, None, NotGiven] = NOT_GIVEN,
environment_id: Union[str, None, NotGiven] = NOT_GIVEN,
+ tunnel_id: Union[str, None, NotGiven] = NOT_GIVEN,
name: Union[str, None, NotGiven] = NOT_GIVEN,
workspace_id: Union[str, None, NotGiven] = NOT_GIVEN,
metadata: Union[Dict[str, Any], None, NotGiven] = NOT_GIVEN,
@@ -21756,6 +21774,7 @@ async def create(
*,
agent: Union[str, AgentParams],
environment_id: str,
+ tunnel_id: Union[str, NotGiven] = NOT_GIVEN,
initial_events: List[Union[UserMessageEventParams,
UserDefineOutcomeEventParams,
SystemMessageEventParams]],
@@ -21821,6 +21840,7 @@ async def update(
environment_variables: Union[str, None, NotGiven] = NOT_GIVEN,
description: Union[str, None, NotGiven] = NOT_GIVEN,
environment_id: Union[str, None, NotGiven] = NOT_GIVEN,
+ tunnel_id: Union[str, None, NotGiven] = NOT_GIVEN,
name: Union[str, None, NotGiven] = NOT_GIVEN,
workspace_id: Union[str, None, NotGiven] = NOT_GIVEN,
metadata: Union[Dict[str, Any], None, NotGiven] = NOT_GIVEN,
@@ -24908,6 +24928,7 @@ def create(
*,
agent: Union[str, AgentParams, AgentWithOverridesParams],
environment_id: str,
+ tunnel_id: Union[str, NotGiven] = NOT_GIVEN,
environment_variables: Union[Dict[str, str], None,
NotGiven] = NOT_GIVEN,
title: Union[str, None, NotGiven] = NOT_GIVEN,
@@ -25178,6 +25199,7 @@ async def create(
*,
agent: Union[str, AgentParams, AgentWithOverridesParams],
environment_id: str,
+ tunnel_id: Union[str, NotGiven] = NOT_GIVEN,
environment_variables: Union[Dict[str, str], None,
NotGiven] = NOT_GIVEN,
title: Union[str, None, NotGiven] = NOT_GIVEN,
@@ -28965,6 +28987,10 @@ class Deployment(BaseModel)
#### environment\_id
+
+
+#### tunnel\_id
+
#### initial\_events
@@ -29057,6 +29083,10 @@ class DeploymentCreateParams(TypedDict)
#### environment\_id
+
+
+#### tunnel\_id
+
#### initial\_events
@@ -29633,6 +29663,10 @@ class DeploymentUpdateParams(TypedDict)
#### environment\_id
+
+
+#### tunnel\_id
+
#### name
@@ -34541,6 +34575,10 @@ class SessionCreateParams(TypedDict)
#### environment\_id
+
+
+#### tunnel\_id
+
#### title
diff --git a/src/qca/forward/resources/templates.py b/src/qca/forward/resources/templates.py
index 9039407..cdc7810 100644
--- a/src/qca/forward/resources/templates.py
+++ b/src/qca/forward/resources/templates.py
@@ -53,6 +53,7 @@ def create(
name: str,
model: Union[str, ModelConfigParam],
environment_id: str,
+ tunnel_id: Union[str, NotGiven] = NOT_GIVEN,
description: Union[str, None, NotGiven] = NOT_GIVEN,
system: Union[str, None, NotGiven] = NOT_GIVEN,
tools: Union[List[ToolParam], None, NotGiven] = NOT_GIVEN,
@@ -80,6 +81,7 @@ def create(
"name": name,
"model": model,
"environment_id": environment_id,
+ "tunnel_id": tunnel_id,
"description": description,
"system": system,
"tools": tools,
@@ -140,6 +142,7 @@ def update(
max_tool_rounds: Union[int, None, NotGiven] = NOT_GIVEN,
managed_tool_config: Union[ManagedToolConfigParam, None, NotGiven] = NOT_GIVEN,
environment_id: Union[str, None, NotGiven] = NOT_GIVEN,
+ tunnel_id: Union[str, None, NotGiven] = NOT_GIVEN,
vaults: Union[Dict[str, ResourceBindingParam], None, NotGiven] = NOT_GIVEN,
files: Union[Dict[str, ResourceBindingParam], None, NotGiven] = NOT_GIVEN,
github_repositories: Union[Dict[str, GitHubRepositoryParam], None, NotGiven] = NOT_GIVEN,
@@ -167,6 +170,7 @@ def update(
"max_tool_rounds": max_tool_rounds,
"managed_tool_config": managed_tool_config,
"environment_id": environment_id,
+ "tunnel_id": tunnel_id,
"vaults": vaults,
"files": files,
"github_repositories": github_repositories,
@@ -265,6 +269,7 @@ async def create(
name: str,
model: Union[str, ModelConfigParam],
environment_id: str,
+ tunnel_id: Union[str, NotGiven] = NOT_GIVEN,
description: Union[str, None, NotGiven] = NOT_GIVEN,
system: Union[str, None, NotGiven] = NOT_GIVEN,
tools: Union[List[ToolParam], None, NotGiven] = NOT_GIVEN,
@@ -292,6 +297,7 @@ async def create(
"name": name,
"model": model,
"environment_id": environment_id,
+ "tunnel_id": tunnel_id,
"description": description,
"system": system,
"tools": tools,
@@ -352,6 +358,7 @@ async def update(
max_tool_rounds: Union[int, None, NotGiven] = NOT_GIVEN,
managed_tool_config: Union[ManagedToolConfigParam, None, NotGiven] = NOT_GIVEN,
environment_id: Union[str, None, NotGiven] = NOT_GIVEN,
+ tunnel_id: Union[str, None, NotGiven] = NOT_GIVEN,
vaults: Union[Dict[str, ResourceBindingParam], None, NotGiven] = NOT_GIVEN,
files: Union[Dict[str, ResourceBindingParam], None, NotGiven] = NOT_GIVEN,
github_repositories: Union[Dict[str, GitHubRepositoryParam], None, NotGiven] = NOT_GIVEN,
@@ -379,6 +386,7 @@ async def update(
"max_tool_rounds": max_tool_rounds,
"managed_tool_config": managed_tool_config,
"environment_id": environment_id,
+ "tunnel_id": tunnel_id,
"vaults": vaults,
"files": files,
"github_repositories": github_repositories,
diff --git a/src/qca/forward/types/template.py b/src/qca/forward/types/template.py
index a0b7a11..2a11cdb 100644
--- a/src/qca/forward/types/template.py
+++ b/src/qca/forward/types/template.py
@@ -31,6 +31,7 @@ class Template(BaseModel):
model: Optional[Union[str, ModelConfig]] = None
multiagent: Optional[MultiagentConfig] = None
environment_id: Optional[str] = None
+ tunnel_id: Optional[str] = None
vaults: Optional[Dict[str, ResourceBinding]] = None
files: Optional[Dict[str, ResourceBinding]] = None
github_repositories: Optional[Dict[str, GitHubRepository]] = None
diff --git a/src/qca/forward/types/template_create_params.py b/src/qca/forward/types/template_create_params.py
index b64c457..6a52d55 100644
--- a/src/qca/forward/types/template_create_params.py
+++ b/src/qca/forward/types/template_create_params.py
@@ -23,6 +23,7 @@ class TemplateCreateParams(TypedDict, total=False):
name: Required[str]
model: Required[Union[str, ModelConfigParam]]
environment_id: Required[str]
+ tunnel_id: str
description: Optional[str]
system: Optional[str]
tools: List[ToolParam]
diff --git a/src/qca/forward/types/template_update_params.py b/src/qca/forward/types/template_update_params.py
index c77e594..6286393 100644
--- a/src/qca/forward/types/template_update_params.py
+++ b/src/qca/forward/types/template_update_params.py
@@ -29,6 +29,7 @@ class TemplateUpdateParams(TypedDict, total=False):
skills: List[SkillBindingParam]
multiagent: MultiagentConfigParam
environment_id: Optional[str]
+ tunnel_id: Optional[str]
vaults: Dict[str, ResourceBindingParam]
files: Dict[str, ResourceBindingParam]
github_repositories: Dict[str, GitHubRepositoryParam]
diff --git a/src/qca/managed/resources/deployments.py b/src/qca/managed/resources/deployments.py
index 29a7b0d..5405c1a 100644
--- a/src/qca/managed/resources/deployments.py
+++ b/src/qca/managed/resources/deployments.py
@@ -37,6 +37,7 @@ def create(
*,
agent: Union[str, AgentParams],
environment_id: str,
+ tunnel_id: Union[str, NotGiven] = NOT_GIVEN,
initial_events: List[Union[UserMessageEventParams, UserDefineOutcomeEventParams, SystemMessageEventParams]],
name: str,
environment_variables: Union[str, None, NotGiven] = NOT_GIVEN,
@@ -71,6 +72,7 @@ def create(
"environment_variables": environment_variables,
"agent": agent,
"environment_id": environment_id,
+ "tunnel_id": tunnel_id,
"initial_events": initial_events,
"name": name,
"description": description,
@@ -120,6 +122,7 @@ def update(
environment_variables: Union[str, None, NotGiven] = NOT_GIVEN,
description: Union[str, None, NotGiven] = NOT_GIVEN,
environment_id: Union[str, None, NotGiven] = NOT_GIVEN,
+ tunnel_id: Union[str, None, NotGiven] = NOT_GIVEN,
name: Union[str, None, NotGiven] = NOT_GIVEN,
workspace_id: Union[str, None, NotGiven] = NOT_GIVEN,
metadata: Union[Dict[str, Any], None, NotGiven] = NOT_GIVEN,
@@ -155,6 +158,7 @@ def update(
"environment_variables": environment_variables,
"description": description,
"environment_id": environment_id,
+ "tunnel_id": tunnel_id,
"name": name,
"metadata": metadata,
"resources": resources,
@@ -370,6 +374,7 @@ async def create(
*,
agent: Union[str, AgentParams],
environment_id: str,
+ tunnel_id: Union[str, NotGiven] = NOT_GIVEN,
initial_events: List[Union[UserMessageEventParams, UserDefineOutcomeEventParams, SystemMessageEventParams]],
name: str,
environment_variables: Union[str, None, NotGiven] = NOT_GIVEN,
@@ -404,6 +409,7 @@ async def create(
"environment_variables": environment_variables,
"agent": agent,
"environment_id": environment_id,
+ "tunnel_id": tunnel_id,
"initial_events": initial_events,
"name": name,
"description": description,
@@ -453,6 +459,7 @@ async def update(
environment_variables: Union[str, None, NotGiven] = NOT_GIVEN,
description: Union[str, None, NotGiven] = NOT_GIVEN,
environment_id: Union[str, None, NotGiven] = NOT_GIVEN,
+ tunnel_id: Union[str, None, NotGiven] = NOT_GIVEN,
name: Union[str, None, NotGiven] = NOT_GIVEN,
workspace_id: Union[str, None, NotGiven] = NOT_GIVEN,
metadata: Union[Dict[str, Any], None, NotGiven] = NOT_GIVEN,
@@ -488,6 +495,7 @@ async def update(
"environment_variables": environment_variables,
"description": description,
"environment_id": environment_id,
+ "tunnel_id": tunnel_id,
"name": name,
"metadata": metadata,
"resources": resources,
diff --git a/src/qca/managed/resources/sessions/sessions.py b/src/qca/managed/resources/sessions/sessions.py
index f1afdae..e2766b3 100644
--- a/src/qca/managed/resources/sessions/sessions.py
+++ b/src/qca/managed/resources/sessions/sessions.py
@@ -76,6 +76,7 @@ def create(
*,
agent: Union[str, AgentParams, AgentWithOverridesParams],
environment_id: str,
+ tunnel_id: Union[str, NotGiven] = NOT_GIVEN,
environment_variables: Union[Dict[str, str], None, NotGiven] = NOT_GIVEN,
title: Union[str, None, NotGiven] = NOT_GIVEN,
workspace_id: Union[str, None, NotGiven] = NOT_GIVEN,
@@ -110,6 +111,7 @@ def create(
"environment_variables": environment_variables,
"agent": agent,
"environment_id": environment_id,
+ "tunnel_id": tunnel_id,
"title": title,
"budget": budget,
"initial_events": initial_events,
@@ -394,6 +396,7 @@ async def create(
*,
agent: Union[str, AgentParams, AgentWithOverridesParams],
environment_id: str,
+ tunnel_id: Union[str, NotGiven] = NOT_GIVEN,
environment_variables: Union[Dict[str, str], None, NotGiven] = NOT_GIVEN,
title: Union[str, None, NotGiven] = NOT_GIVEN,
workspace_id: Union[str, None, NotGiven] = NOT_GIVEN,
@@ -428,6 +431,7 @@ async def create(
"environment_variables": environment_variables,
"agent": agent,
"environment_id": environment_id,
+ "tunnel_id": tunnel_id,
"title": title,
"budget": budget,
"initial_events": initial_events,
diff --git a/src/qca/managed/types/deployment.py b/src/qca/managed/types/deployment.py
index 431eacf..21e2ac7 100644
--- a/src/qca/managed/types/deployment.py
+++ b/src/qca/managed/types/deployment.py
@@ -24,6 +24,7 @@ class Deployment(BaseModel):
created_at: Optional[datetime] = None
description: Optional[str] = None
environment_id: Optional[str] = None
+ tunnel_id: Optional[str] = None
initial_events: Optional[List[DeploymentInitialEventUnion]] = None
metadata: Optional[Dict[str, str]] = None
name: Optional[str] = None
diff --git a/src/qca/managed/types/deployment_create_params.py b/src/qca/managed/types/deployment_create_params.py
index 05d8ef7..a255dd6 100644
--- a/src/qca/managed/types/deployment_create_params.py
+++ b/src/qca/managed/types/deployment_create_params.py
@@ -23,6 +23,7 @@ class DeploymentCreateParams(TypedDict, total=False):
environment_variables: Optional[str]
agent: Required[Union[str, AgentParams]]
environment_id: Required[str]
+ tunnel_id: str
initial_events: Required[
List[Union[UserMessageEventParams, UserDefineOutcomeEventParams, SystemMessageEventParams]]
]
diff --git a/src/qca/managed/types/deployment_update_params.py b/src/qca/managed/types/deployment_update_params.py
index 713f0e2..bb28aea 100644
--- a/src/qca/managed/types/deployment_update_params.py
+++ b/src/qca/managed/types/deployment_update_params.py
@@ -23,6 +23,7 @@ class DeploymentUpdateParams(TypedDict, total=False):
environment_variables: Optional[str]
description: Optional[str]
environment_id: Optional[str]
+ tunnel_id: Optional[str]
name: Optional[str]
workspace_id: Optional[str]
metadata: Dict[str, Any]
diff --git a/src/qca/managed/types/session_create_params.py b/src/qca/managed/types/session_create_params.py
index ce58788..cccf9f8 100644
--- a/src/qca/managed/types/session_create_params.py
+++ b/src/qca/managed/types/session_create_params.py
@@ -22,6 +22,7 @@ class SessionCreateParams(TypedDict, total=False):
environment_variables: Dict[str, str]
agent: Required[Union[str, AgentParams, AgentWithOverridesParams]]
environment_id: Required[str]
+ tunnel_id: str
title: Optional[str]
workspace_id: Optional[str]
budget: BudgetLimitParam
diff --git a/tests/test_byoc_tunnel.py b/tests/test_byoc_tunnel.py
new file mode 100644
index 0000000..c0e8da8
--- /dev/null
+++ b/tests/test_byoc_tunnel.py
@@ -0,0 +1,62 @@
+import inspect
+import json
+
+import httpx
+import pytest
+
+from qca import AsyncForward, AsyncManaged, Forward, Managed
+
+
+async def resolve(value):
+ return await value if inspect.isawaitable(value) else value
+
+
+@pytest.mark.parametrize("async_", [False, True])
+async def test_byoc_tunnel_binding(async_):
+ requests = []
+
+ def handle(request):
+ body = json.loads(request.content)
+ requests.append(body)
+ response = {"id": "test"}
+ if body.get("tunnel_id"):
+ response["tunnel_id"] = body["tunnel_id"]
+ return httpx.Response(200, json=response)
+
+ http = httpx.AsyncClient if async_ else httpx.Client
+ forward = (AsyncForward if async_ else Forward)(pat="test", http_client=http(transport=httpx.MockTransport(handle)))
+ managed = (AsyncManaged if async_ else Managed)(pat="test", http_client=http(transport=httpx.MockTransport(handle)))
+ try:
+ for binding in ({}, {"tunnel_id": "tnl_test"}):
+ await resolve(managed.sessions.create(agent="agent_test", environment_id="env_test", **binding))
+ assert requests[-1] == {
+ "agent": "agent_test",
+ "environment_id": "env_test",
+ "metadata": requests[-1]["metadata"],
+ **binding,
+ }
+ template = await resolve(
+ forward.templates.create(name="demo", model="auto", environment_id="env_test", **binding)
+ )
+ assert requests[-1] == {"name": "demo", "model": "auto", "environment_id": "env_test", **binding}
+ assert template.tunnel_id == binding.get("tunnel_id")
+ assert "tunnel_id" in type(template).model_fields
+ deployment_params = {
+ "name": "demo",
+ "agent": "agent_test",
+ "environment_id": "env_test",
+ "initial_events": [{"type": "user.message", "content": [{"type": "text", "text": "hello"}]}],
+ **binding,
+ }
+ deployment = await resolve(managed.deployments.create(**deployment_params))
+ assert requests[-1] == deployment_params
+ assert deployment.tunnel_id == binding.get("tunnel_id")
+ assert "tunnel_id" in type(deployment).model_fields
+ for patch in ({}, {"tunnel_id": "tnl_other"}, {"tunnel_id": None}):
+ await resolve(forward.templates.update("tmpl_test", **patch))
+ assert requests[-1] == patch
+ await resolve(managed.deployments.update("dep_test", **patch))
+ assert requests[-1] == patch
+ finally:
+ await resolve(managed.close())
+ await resolve(forward.close())