agentengine-sdk-python 0.8.7__py3-none-any.whl → 0.8.8__py3-none-any.whl
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.
- {agentengine_sdk_python-0.8.7.dist-info → agentengine_sdk_python-0.8.8.dist-info}/METADATA +14 -9
- {agentengine_sdk_python-0.8.7.dist-info → agentengine_sdk_python-0.8.8.dist-info}/RECORD +98 -68
- ksadk/__init__.py +2 -1
- ksadk/_build_provenance.json +1 -1
- ksadk/_mcp_compat.py +305 -0
- ksadk/agent.py +29 -0
- ksadk/agui/_agent_kernel.py +176 -142
- ksadk/agui/agent.py +74 -5
- ksadk/agui/hosted.py +343 -0
- ksadk/agui/request_identity.py +11 -0
- ksadk/agui/routes.py +77 -2
- ksadk/builders/code_builder.py +30 -23
- ksadk/builders/container_builder.py +5 -5
- ksadk/builders/mcp_builder.py +142 -0
- ksadk/builders/project_imports.py +66 -0
- ksadk/cli/cmd_mcp.py +1 -1
- ksadk/cli/cmd_run.py +2 -4
- ksadk/cli/cmd_web.py +8 -0
- ksadk/cli/runtime_bootstrap.py +4 -1
- ksadk/common/cloud_headers.py +33 -6
- ksadk/compat/adk_compat.py +2 -0
- ksadk/compat/adk_stream.py +35 -0
- ksadk/configs/env_registry.py +31 -0
- ksadk/conversations/projector.py +6 -0
- ksadk/conversations/runtime_observability.py +1 -0
- ksadk/detection/mcp_detector.py +80 -22
- ksadk/harness/__init__.py +3 -0
- ksadk/harness/service_presentation.py +44 -0
- ksadk/harness/service_runtime.py +1038 -0
- ksadk/interaction/providers/harness.py +9 -4
- ksadk/kernel/ingress.py +35 -25
- ksadk/kernel/worker.py +2 -2
- ksadk/kernel/worker_identity.py +15 -8
- ksadk/knowledge_base/client.py +10 -3
- ksadk/mcp_runtime/oauth/_agent.py +77 -9
- ksadk/mcp_runtime/oauth/langgraph.py +47 -15
- ksadk/mcp_runtime/oauth/local/broker.py +23 -1
- ksadk/mcp_runtime/oauth/tools.py +46 -8
- ksadk/mcp_runtime/oauth/transport.py +19 -2
- ksadk/memory/adk/backends/sdk_ltm_backend.py +21 -12
- ksadk/memory/scope_ids.py +49 -0
- ksadk/plugins/providers/harness.py +65 -28
- ksadk/plugins/providers/harness_service.py +198 -0
- ksadk/plugins/providers/harness_service_context.py +175 -0
- ksadk/plugins/providers/harness_service_hooks.py +168 -0
- ksadk/plugins/providers/harness_service_memory.py +479 -0
- ksadk/plugins/providers/platform_resources.py +2 -2
- ksadk/resource_runtime/langgraph.py +7 -3
- ksadk/resource_runtime/mcp_server.py +3 -3
- ksadk/resource_runtime/worker.py +19 -4
- ksadk/runners/_adk_canonical_stream.py +38 -10
- ksadk/runners/_adk_tunnel_history.py +96 -0
- ksadk/runners/_langgraph_canonical_stream.py +16 -0
- ksadk/runners/_langgraph_factory.py +79 -0
- ksadk/runners/_langgraph_runner_streams.py +6 -0
- ksadk/runners/_session_identity.py +8 -3
- ksadk/runners/adk_runner.py +95 -19
- ksadk/runners/langgraph_runner.py +15 -21
- ksadk/runtime/factory.py +17 -5
- ksadk/runtime/framework_adapters.py +10 -0
- ksadk/runtime/langgraph_checkpoint_view.py +28 -7
- ksadk/runtime/launch.py +2 -0
- ksadk/server/factory.py +55 -0
- ksadk/server/routes/common.py +2 -0
- ksadk/server/routes/harness_service_view.py +38 -0
- ksadk/server/routes/ui_bootstrap.py +16 -2
- ksadk/skills/mcp_server/server.py +239 -217
- ksadk/studio/contracts.py +8 -0
- ksadk/studio/mcp_runtime.py +23 -8
- ksadk/studio/native_mcp_disclosure.py +8 -6
- ksadk/studio/native_mcp_tool_port.py +3 -1
- ksadk/studio/native_memory_context.py +10 -8
- ksadk/studio/native_product.py +3 -4
- ksadk/studio/plugin_composition.py +8 -1
- ksadk/studio/plugin_native_activation.py +4 -2
- ksadk/studio/plugin_runtime.py +9 -0
- ksadk/studio/service.py +2 -0
- ksadk/tracing/setup.py +53 -3
- ksadk/tunnel/__init__.py +37 -0
- ksadk/tunnel/connector.py +384 -0
- ksadk/tunnel/discovery.py +107 -0
- ksadk/tunnel/failure.py +168 -0
- ksadk/tunnel/http_tools.py +100 -0
- ksadk/tunnel/langgraph.py +125 -0
- ksadk/tunnel/middleware.py +28 -0
- ksadk/tunnel/peers.py +872 -0
- ksadk/tunnel/protocol.py +97 -0
- ksadk/tunnel/registry.py +306 -0
- ksadk/tunnel/runtime.py +147 -0
- ksadk/tunnel/server.py +201 -0
- ksadk/tunnel/services.py +366 -0
- ksadk/tunnel/toolset.py +78 -0
- ksadk/version.py +1 -1
- ksadk_runtime_common/memory_backend/providers/mem0.py +20 -0
- {agentengine_sdk_python-0.8.7.dist-info → agentengine_sdk_python-0.8.8.dist-info}/WHEEL +0 -0
- {agentengine_sdk_python-0.8.7.dist-info → agentengine_sdk_python-0.8.8.dist-info}/entry_points.txt +0 -0
- {agentengine_sdk_python-0.8.7.dist-info → agentengine_sdk_python-0.8.8.dist-info}/licenses/LICENSE +0 -0
- {agentengine_sdk_python-0.8.7.dist-info → agentengine_sdk_python-0.8.8.dist-info}/top_level.txt +0 -0
ksadk/_mcp_compat.py
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
"""``mcp`` SDK 1.x / 2.x 的 Server 类兼容导入。
|
|
2
|
+
|
|
3
|
+
## 为什么需要
|
|
4
|
+
|
|
5
|
+
`mcp` **2.3.0** 把 ``FastMCP`` 改名成 ``MCPServer``,挪到
|
|
6
|
+
``mcp.server.mcpserver``,并**删除**了 ``mcp.server.fastmcp``。
|
|
7
|
+
|
|
8
|
+
平台镜像和用户环境里可能装着任一版本(ksadk 的 ``mcp`` 约束是
|
|
9
|
+
``>=2.3.0``,但下游可能锁旧版)。硬编码一条路径会在另一个版本上
|
|
10
|
+
``ModuleNotFoundError``——而 MCP Server 起不来时**只是工具不可用**,
|
|
11
|
+
错误信息也指向不到根因,很难定位。
|
|
12
|
+
|
|
13
|
+
## 用法
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from ksadk._mcp_compat import MCPServer
|
|
17
|
+
```
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"MCPServer",
|
|
24
|
+
"httpx_module",
|
|
25
|
+
"mcp_version",
|
|
26
|
+
"read_timeout_seconds",
|
|
27
|
+
"streamable_http",
|
|
28
|
+
"field",
|
|
29
|
+
"list_tools_page",
|
|
30
|
+
"tool_input_schema",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
import contextlib
|
|
34
|
+
import importlib.metadata as _md
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def httpx_module():
|
|
38
|
+
"""返回 **mcp 当前使用的那个 httpx 模块**。
|
|
39
|
+
|
|
40
|
+
## 为什么要这个
|
|
41
|
+
|
|
42
|
+
`mcp` 2.x 换成了独立的 `httpx2` 包(**不是** `httpx` 的子类)。
|
|
43
|
+
任何给 `streamable_http_client(http_client=...)` 传客户端的地方都必须
|
|
44
|
+
用**同一套** httpx,否则类型不匹配——实测表现是**静默挂起**(不是报错),
|
|
45
|
+
排查成本极高。
|
|
46
|
+
|
|
47
|
+
## 为什么不能按函数名判断
|
|
48
|
+
|
|
49
|
+
实测(2026-11-19)**mcp 1.26 同时导出了 `streamablehttp_client`(旧)
|
|
50
|
+
和 `streamable_http_client`(新)**,所以「能不能 import 新名字」
|
|
51
|
+
**不能**区分版本——1.26 会被误判成 2.x,进而要求不存在的 `httpx2`。
|
|
52
|
+
|
|
53
|
+
改成看 `http_client` 参数的**类型注解**:
|
|
54
|
+
|
|
55
|
+
| mcp | 注解 |
|
|
56
|
+
|---|---|
|
|
57
|
+
| 1.26 | ``httpx.AsyncClient`` |
|
|
58
|
+
| 2.3 | ``httpx2.AsyncClient`` |
|
|
59
|
+
"""
|
|
60
|
+
import inspect
|
|
61
|
+
|
|
62
|
+
try:
|
|
63
|
+
from mcp.client.streamable_http import streamable_http_client
|
|
64
|
+
except ImportError: # pragma: no cover - 极旧版本
|
|
65
|
+
import httpx
|
|
66
|
+
|
|
67
|
+
return httpx
|
|
68
|
+
|
|
69
|
+
parameter = inspect.signature(streamable_http_client).parameters.get("http_client")
|
|
70
|
+
annotation = str(getattr(parameter, "annotation", ""))
|
|
71
|
+
if "httpx2" in annotation:
|
|
72
|
+
import httpx2
|
|
73
|
+
|
|
74
|
+
return httpx2
|
|
75
|
+
|
|
76
|
+
import httpx
|
|
77
|
+
|
|
78
|
+
return httpx
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def field(obj: object, *names: str, default=None):
|
|
82
|
+
"""按顺序取第一个非 None 的属性/键,兼容 mcp 1.x 的 camelCase 与 2.x 的 snake_case。
|
|
83
|
+
|
|
84
|
+
## 为什么需要
|
|
85
|
+
|
|
86
|
+
`mcp` 2.x 把**所有** Pydantic 模型字段从 camelCase 改成 snake_case:
|
|
87
|
+
|
|
88
|
+
| 1.x | 2.x |
|
|
89
|
+
|---|---|
|
|
90
|
+
| ``result.isError`` | ``result.is_error`` |
|
|
91
|
+
| ``result.structuredContent`` | ``result.structured_content`` |
|
|
92
|
+
| ``page.nextCursor`` | ``page.next_cursor`` |
|
|
93
|
+
| ``tool.inputSchema`` | ``tool.input_schema`` |
|
|
94
|
+
|
|
95
|
+
这些**只在运行时炸**(Pydantic 不做静态检查),且错误信息(`AttributeError`)
|
|
96
|
+
指向不到根因。
|
|
97
|
+
|
|
98
|
+
## 用法
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
field(result, "is_error", "isError", default=False)
|
|
102
|
+
```
|
|
103
|
+
"""
|
|
104
|
+
for name in names:
|
|
105
|
+
value = getattr(obj, name, None)
|
|
106
|
+
if value is not None:
|
|
107
|
+
return value
|
|
108
|
+
if isinstance(obj, dict):
|
|
109
|
+
for name in names:
|
|
110
|
+
value = obj.get(name)
|
|
111
|
+
if value is not None:
|
|
112
|
+
return value
|
|
113
|
+
return default
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
async def list_tools_page(session, cursor: str | None = None):
|
|
117
|
+
"""分页列工具,兼容 1.x / 2.x 的分页参数形态。
|
|
118
|
+
|
|
119
|
+
## 为什么要这个
|
|
120
|
+
|
|
121
|
+
| | 签名 |
|
|
122
|
+
|---|---|
|
|
123
|
+
| mcp 1.x | ``list_tools(cursor=None)`` |
|
|
124
|
+
| mcp 2.x | ``list_tools(*, params=PaginatedRequestParams(cursor=...))`` |
|
|
125
|
+
|
|
126
|
+
传错直接 ``TypeError: got an unexpected keyword argument 'cursor'``,
|
|
127
|
+
而且被隧道兜底成 ``local_service_error``,看不到根因。
|
|
128
|
+
"""
|
|
129
|
+
try:
|
|
130
|
+
return await session.list_tools(cursor=cursor)
|
|
131
|
+
except TypeError:
|
|
132
|
+
from mcp.types import PaginatedRequestParams
|
|
133
|
+
|
|
134
|
+
params = PaginatedRequestParams(cursor=cursor) if cursor else None
|
|
135
|
+
return await session.list_tools(params=params)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def read_timeout_seconds(seconds: float):
|
|
139
|
+
"""把「秒数」转成 mcp 期望的 ``read_timeout_seconds`` 类型。
|
|
140
|
+
|
|
141
|
+
## 为什么要这个
|
|
142
|
+
|
|
143
|
+
`ClientSession.read_timeout_seconds` 的类型**在 2.x 变了**:
|
|
144
|
+
|
|
145
|
+
| | 类型 |
|
|
146
|
+
|---|---|
|
|
147
|
+
| mcp 1.x | ``datetime.timedelta`` |
|
|
148
|
+
| mcp 2.x | ``float``(秒) |
|
|
149
|
+
|
|
150
|
+
传错会在**每次请求**时炸在 mcp 内部:
|
|
151
|
+
|
|
152
|
+
anyio.current_time() + self._budget
|
|
153
|
+
TypeError: unsupported operand type(s) for +: 'float' and 'datetime.timedelta'
|
|
154
|
+
|
|
155
|
+
注意这个错误**不在我们的栈里**(在 `mcp/shared/_request_clock.py`),
|
|
156
|
+
不看完整 traceback 根本定位不到。
|
|
157
|
+
|
|
158
|
+
## 判定方式
|
|
159
|
+
|
|
160
|
+
用**注解**而不是版本号——`inspect.signature` 拿到的类型是权威的,
|
|
161
|
+
不受预发布版本字符串格式影响。
|
|
162
|
+
"""
|
|
163
|
+
import inspect
|
|
164
|
+
|
|
165
|
+
from mcp.client.session import ClientSession
|
|
166
|
+
|
|
167
|
+
parameter = inspect.signature(ClientSession.__init__).parameters.get(
|
|
168
|
+
"read_timeout_seconds"
|
|
169
|
+
)
|
|
170
|
+
annotation = str(getattr(parameter, "annotation", ""))
|
|
171
|
+
|
|
172
|
+
if "timedelta" in annotation:
|
|
173
|
+
from datetime import timedelta
|
|
174
|
+
|
|
175
|
+
return timedelta(seconds=float(seconds))
|
|
176
|
+
return float(seconds)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def tool_input_schema(tool: object) -> dict:
|
|
180
|
+
"""取 MCP 工具的 input schema,兼容 1.x / 2.x 的字段名。
|
|
181
|
+
|
|
182
|
+
## 为什么要这个
|
|
183
|
+
|
|
184
|
+
`mcp` 2.x 把 Pydantic 模型字段从 **camelCase 改成 snake_case**:
|
|
185
|
+
|
|
186
|
+
| | 1.x | 2.x |
|
|
187
|
+
|---|---|---|
|
|
188
|
+
| 属性 | ``tool.inputSchema`` | ``tool.input_schema`` |
|
|
189
|
+
| ``model_dump()`` 键 | ``"inputSchema"`` | ``"input_schema"`` |
|
|
190
|
+
|
|
191
|
+
直接取属性会 `AttributeError`,直接取 dict 键会 `KeyError`,
|
|
192
|
+
而这两个都是**运行时**才炸(Pydantic 不做静态检查),只能在测试里发现。
|
|
193
|
+
"""
|
|
194
|
+
for name in ("input_schema", "inputSchema"):
|
|
195
|
+
value = getattr(tool, name, None)
|
|
196
|
+
if value is not None:
|
|
197
|
+
return dict(value)
|
|
198
|
+
if isinstance(tool, dict):
|
|
199
|
+
for name in ("input_schema", "inputSchema"):
|
|
200
|
+
value = tool.get(name)
|
|
201
|
+
if value is not None:
|
|
202
|
+
return dict(value)
|
|
203
|
+
return {}
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def mcp_version() -> str:
|
|
207
|
+
"""当前安装的 ``mcp`` 版本(诊断用)。"""
|
|
208
|
+
try:
|
|
209
|
+
return _md.version("mcp")
|
|
210
|
+
except _md.PackageNotFoundError: # pragma: no cover
|
|
211
|
+
return "unknown"
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
try: # mcp >= 2.3
|
|
215
|
+
from mcp.server.mcpserver import MCPServer
|
|
216
|
+
except ImportError: # pragma: no cover - 兼容旧版
|
|
217
|
+
try: # mcp 2.0 ~ 2.2 的内部路径
|
|
218
|
+
from mcp.server.mcpserver.server import MCPServer # type: ignore[no-redef]
|
|
219
|
+
except ImportError: # mcp 1.x
|
|
220
|
+
from mcp.server.fastmcp import FastMCP as MCPServer # type: ignore[no-redef]
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def streamable_http(
|
|
224
|
+
url: str,
|
|
225
|
+
headers: dict[str, str] | None = None,
|
|
226
|
+
*,
|
|
227
|
+
http_client: object | None = None,
|
|
228
|
+
httpx_client_factory: object | None = None,
|
|
229
|
+
timeout: float | None = None,
|
|
230
|
+
):
|
|
231
|
+
"""兼容 mcp 1.x / 2.x 的 Streamable HTTP 传输。
|
|
232
|
+
|
|
233
|
+
## 为什么要包
|
|
234
|
+
|
|
235
|
+
两个版本的**函数名和签名都不同**:
|
|
236
|
+
|
|
237
|
+
| | 1.x | 2.x |
|
|
238
|
+
|---|---|---|
|
|
239
|
+
| 函数名 | ``streamablehttp_client`` | ``streamable_http_client`` |
|
|
240
|
+
| 请求头 | ``headers={...}`` | 无此参数,要用 ``http_client=httpx2.AsyncClient(headers=...)`` |
|
|
241
|
+
| 返回值 | ``(read, write, get_session_id)`` | ``TransportStreams`` |
|
|
242
|
+
|
|
243
|
+
## 用法
|
|
244
|
+
|
|
245
|
+
统一成 **``(read, write)`` 二元组**,两个版本写法一致:
|
|
246
|
+
|
|
247
|
+
```python
|
|
248
|
+
async with streamable_http(url, {"Authorization": "..."}) as (read, write):
|
|
249
|
+
async with ClientSession(read, write) as session:
|
|
250
|
+
...
|
|
251
|
+
```
|
|
252
|
+
"""
|
|
253
|
+
if not url:
|
|
254
|
+
raise ValueError("url 不能为空")
|
|
255
|
+
|
|
256
|
+
try: # mcp >= 2.3
|
|
257
|
+
from mcp.client.streamable_http import streamable_http_client
|
|
258
|
+
except ImportError: # pragma: no cover - mcp 1.x
|
|
259
|
+
from mcp.client.streamable_http import streamablehttp_client
|
|
260
|
+
|
|
261
|
+
return _normalize(
|
|
262
|
+
streamablehttp_client(
|
|
263
|
+
url,
|
|
264
|
+
headers=headers,
|
|
265
|
+
httpx_client_factory=httpx_client_factory, # type: ignore[arg-type]
|
|
266
|
+
timeout=timeout if timeout is not None else 30,
|
|
267
|
+
)
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
# mcp 2.x 没有 `headers` / `httpx_client_factory` / `timeout` 参数,
|
|
271
|
+
# 统一收敛到「传一个配好的 http_client」。
|
|
272
|
+
client = http_client
|
|
273
|
+
if client is None and httpx_client_factory is not None:
|
|
274
|
+
# 调用方按 1.x 习惯传工厂——直接调用拿到 client
|
|
275
|
+
client = httpx_client_factory(headers=headers, timeout=timeout) # type: ignore[operator]
|
|
276
|
+
if client is None:
|
|
277
|
+
# mcp 1.26 also exports this spelling, but still uses httpx.
|
|
278
|
+
client = httpx_module().AsyncClient(
|
|
279
|
+
headers=headers or {},
|
|
280
|
+
**({"timeout": timeout} if timeout is not None else {}),
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
return _normalize(streamable_http_client(url, http_client=client)) # type: ignore[arg-type]
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
@contextlib.asynccontextmanager
|
|
287
|
+
async def _normalize(streams):
|
|
288
|
+
"""把传输流统一裁成 ``(read, write)`` 二元组。
|
|
289
|
+
|
|
290
|
+
mcp 2.x 的 ``TransportStreams`` 在不同小版本里**既可能是具名对象**
|
|
291
|
+
(``.read`` / ``.write``),**也可能是普通元组**——实测 2.3.0 是
|
|
292
|
+
``tuple[ReadStream, WriteStream]``。统一裁成二元组,调用方写法固定。
|
|
293
|
+
"""
|
|
294
|
+
if hasattr(streams, "__aenter__"):
|
|
295
|
+
async with streams as inner:
|
|
296
|
+
if hasattr(inner, "read"):
|
|
297
|
+
yield inner.read, inner.write
|
|
298
|
+
else:
|
|
299
|
+
# mcp 1.x 返回 3 元组 ``(read, write, get_session_id)``
|
|
300
|
+
yield inner[0], inner[1]
|
|
301
|
+
elif hasattr(streams, "read"):
|
|
302
|
+
yield streams.read, streams.write
|
|
303
|
+
else:
|
|
304
|
+
# mcp 2.x 返回 2 元组;1.x 返回 3 元组——都只取前两个
|
|
305
|
+
yield streams[0], streams[1]
|
ksadk/agent.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Declare runtime features on the exported framework agent."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import TypeVar
|
|
6
|
+
|
|
7
|
+
T = TypeVar("T")
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True)
|
|
11
|
+
class TunnelDeclaration:
|
|
12
|
+
enabled: bool
|
|
13
|
+
token_env: str
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def Agent(
|
|
17
|
+
agent: T, *, enable_tunnel: bool = False, tunnel_token_env: str = "KSADK_TUNNEL_TOKEN"
|
|
18
|
+
) -> T:
|
|
19
|
+
"""Return the original framework object; the runtime reads the secret at startup."""
|
|
20
|
+
if not isinstance(enable_tunnel, bool):
|
|
21
|
+
raise TypeError("enable_tunnel must be a bool")
|
|
22
|
+
if not isinstance(tunnel_token_env, str) or not re.fullmatch(
|
|
23
|
+
r"[A-Za-z_][A-Za-z_0-9]*", tunnel_token_env
|
|
24
|
+
):
|
|
25
|
+
raise ValueError("tunnel_token_env must be an environment variable name")
|
|
26
|
+
object.__setattr__(
|
|
27
|
+
agent, "_ksadk_tunnel_declaration", TunnelDeclaration(enable_tunnel, tunnel_token_env)
|
|
28
|
+
)
|
|
29
|
+
return agent
|