kleia 0.1.0__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.
- kleia/__init__.py +1 -0
- kleia/agent_list.py +77 -0
- kleia/agent_mcp/__init__.py +10 -0
- kleia/agent_mcp/server.py +403 -0
- kleia/agent_read.py +56 -0
- kleia/agent_relationships.py +33 -0
- kleia/agent_search.py +49 -0
- kleia/agent_setup.py +273 -0
- kleia/agent_status.py +20 -0
- kleia/agent_wait.py +36 -0
- kleia/axi.py +567 -0
- kleia/errors.py +98 -0
- kleia/http.py +41 -0
- kleia/login.py +284 -0
- kleia/oauth.py +161 -0
- kleia/process.py +34 -0
- kleia/profile_store.py +196 -0
- kleia/query_filters.py +99 -0
- kleia/reading_contract.py +62 -0
- kleia/rest.py +543 -0
- kleia/skill.py +67 -0
- kleia/target_resolver.py +80 -0
- kleia/token_store.py +144 -0
- kleia/typer_app.py +702 -0
- kleia/verify_backend.py +91 -0
- kleia/version.py +8 -0
- kleia-0.1.0.dist-info/METADATA +86 -0
- kleia-0.1.0.dist-info/RECORD +31 -0
- kleia-0.1.0.dist-info/WHEEL +4 -0
- kleia-0.1.0.dist-info/entry_points.txt +4 -0
- kleia-0.1.0.dist-info/licenses/LICENSE +18 -0
kleia/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Kleia agent client: CLI and stdio MCP over the Kleia REST API."""
|
kleia/agent_list.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""Formal Agent CLI ``list-concepts`` and ``list-readable`` commands.
|
|
2
|
+
|
|
3
|
+
Both commands wrap the shared REST operations over ``GET /api/documents``.
|
|
4
|
+
``list-readable`` returns that Shared Capability Contract directly.
|
|
5
|
+
``list-concepts`` filters the same listing down to Concepts.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from kleia import rest
|
|
13
|
+
from kleia.target_resolver import resolve_base_url
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def run_list_concepts(
|
|
17
|
+
base_url: str | None,
|
|
18
|
+
profile: str | None = None,
|
|
19
|
+
*,
|
|
20
|
+
type_filter: str | None = None,
|
|
21
|
+
tags: str | None = None,
|
|
22
|
+
path_prefix: str | None = None,
|
|
23
|
+
trust_tier: str | None = None,
|
|
24
|
+
effective_status: str | None = None,
|
|
25
|
+
staleness: str | None = None,
|
|
26
|
+
source_resource: str | None = None,
|
|
27
|
+
runtime: str | None = None,
|
|
28
|
+
) -> dict[str, Any]:
|
|
29
|
+
"""Execute the formal ``list-concepts`` command and return the shared dict."""
|
|
30
|
+
resolved_url = resolve_base_url(base_url, profile=profile)
|
|
31
|
+
try:
|
|
32
|
+
return rest.list_concepts(
|
|
33
|
+
resolved_url,
|
|
34
|
+
type_filter=type_filter,
|
|
35
|
+
tags=tags,
|
|
36
|
+
path_prefix=path_prefix,
|
|
37
|
+
trust_tier=trust_tier,
|
|
38
|
+
effective_status=effective_status,
|
|
39
|
+
staleness=staleness,
|
|
40
|
+
source_resource=source_resource,
|
|
41
|
+
runtime=runtime,
|
|
42
|
+
profile=profile,
|
|
43
|
+
)
|
|
44
|
+
except rest.RestError as exc:
|
|
45
|
+
rest.raise_as_cli(exc, command="list-concepts", base_url=base_url, profile=profile)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def run_list_readable(
|
|
49
|
+
base_url: str | None,
|
|
50
|
+
profile: str | None = None,
|
|
51
|
+
*,
|
|
52
|
+
type_filter: str | None = None,
|
|
53
|
+
tags: str | None = None,
|
|
54
|
+
path_prefix: str | None = None,
|
|
55
|
+
trust_tier: str | None = None,
|
|
56
|
+
effective_status: str | None = None,
|
|
57
|
+
staleness: str | None = None,
|
|
58
|
+
source_resource: str | None = None,
|
|
59
|
+
runtime: str | None = None,
|
|
60
|
+
) -> dict[str, Any]:
|
|
61
|
+
"""Execute the formal ``list-readable`` command and return the shared dict."""
|
|
62
|
+
resolved_url = resolve_base_url(base_url, profile=profile)
|
|
63
|
+
try:
|
|
64
|
+
return rest.list_readable(
|
|
65
|
+
resolved_url,
|
|
66
|
+
type_filter=type_filter,
|
|
67
|
+
tags=tags,
|
|
68
|
+
path_prefix=path_prefix,
|
|
69
|
+
trust_tier=trust_tier,
|
|
70
|
+
effective_status=effective_status,
|
|
71
|
+
staleness=staleness,
|
|
72
|
+
source_resource=source_resource,
|
|
73
|
+
runtime=runtime,
|
|
74
|
+
profile=profile,
|
|
75
|
+
)
|
|
76
|
+
except rest.RestError as exc:
|
|
77
|
+
rest.raise_as_cli(exc, command="list-readable", base_url=base_url, profile=profile)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""Kleia-connected Agent MCP adapter package.
|
|
2
|
+
|
|
3
|
+
Exposes the same consumption capabilities as the ``kleia`` CLI over
|
|
4
|
+
the Model Context Protocol. The adapter resolves a single Kleia
|
|
5
|
+
backend at startup, then registers tools that call that backend over HTTP
|
|
6
|
+
and return JSON envelopes matching the CLI's stdout envelopes 1:1.
|
|
7
|
+
|
|
8
|
+
The ``kleia-mcp`` console script entrypoint lives at
|
|
9
|
+
:func:`kleia.agent_mcp.server.main`.
|
|
10
|
+
"""
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
"""Backend-connected Agent MCP server (TEA-961).
|
|
2
|
+
|
|
3
|
+
Registers agent consumption tools on an :class:`mcp.server.MCPServer`
|
|
4
|
+
and runs them over stdio. The server resolves its backend target once
|
|
5
|
+
at startup using the same layered chain the CLI uses
|
|
6
|
+
(:func:`kleia.target_resolver.resolve_base_url`): explicit
|
|
7
|
+
``--base-url`` argument, then ``KLEIA_AGENT_BASE_URL``, then a named
|
|
8
|
+
``--profile``, then the default profile. No target means **fail-fast**:
|
|
9
|
+
the process writes a single JSON error envelope to stderr and exits.
|
|
10
|
+
|
|
11
|
+
Tools call the shared REST operations in :mod:`kleia.rest` and
|
|
12
|
+
return the same JSON envelopes the CLI writes to stdout. Cross-adapter
|
|
13
|
+
parity lives in ``tests/test_cross_adapter_contract_parity.py``.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import argparse
|
|
19
|
+
import functools
|
|
20
|
+
import sys
|
|
21
|
+
from collections.abc import Callable, Sequence
|
|
22
|
+
from typing import Any, Final
|
|
23
|
+
|
|
24
|
+
from mcp.server import MCPServer
|
|
25
|
+
from mcp.server.mcpserver.exceptions import ToolError
|
|
26
|
+
|
|
27
|
+
from kleia import rest
|
|
28
|
+
from kleia.errors import (
|
|
29
|
+
AgentCliError,
|
|
30
|
+
ErrorCode,
|
|
31
|
+
emit_error,
|
|
32
|
+
)
|
|
33
|
+
from kleia.target_resolver import resolve_base_url
|
|
34
|
+
|
|
35
|
+
AgentToolError = rest.RestError
|
|
36
|
+
tool_diagnostics = rest.diagnostics
|
|
37
|
+
tool_list_concepts = rest.list_concepts
|
|
38
|
+
tool_list_readable = rest.list_readable
|
|
39
|
+
tool_read_concept = rest.read_concept
|
|
40
|
+
tool_read_document = rest.read_document
|
|
41
|
+
tool_read_raw = rest.read_raw
|
|
42
|
+
tool_read_root = rest.read_root
|
|
43
|
+
tool_relationships = rest.relationships
|
|
44
|
+
tool_search = rest.search
|
|
45
|
+
tool_status = rest.status
|
|
46
|
+
tool_wait_until_fresh = rest.wait_until_fresh
|
|
47
|
+
|
|
48
|
+
PROG_NAME: Final[str] = "kleia-mcp"
|
|
49
|
+
"""Console script name; also the MCP server ``name`` reported to clients."""
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def build_server(base_url: str, profile: str | None = None) -> MCPServer:
|
|
53
|
+
"""Construct an :class:`MCPServer` with every agent consumption tool registered.
|
|
54
|
+
|
|
55
|
+
The registered tool names mirror the ADR-0008 vocabulary and the
|
|
56
|
+
tool signatures deliberately spell out ``concept_id`` /
|
|
57
|
+
``document_path`` / ``root`` so the underlying OKF selector
|
|
58
|
+
semantics stay explicit to the MCP client.
|
|
59
|
+
"""
|
|
60
|
+
server = MCPServer(
|
|
61
|
+
name=PROG_NAME,
|
|
62
|
+
instructions=(
|
|
63
|
+
"Kleia agent consumption tools. Tools mirror the `kleia` CLI's "
|
|
64
|
+
"JSON envelopes. Use read_concept for Concepts; use read_document as "
|
|
65
|
+
"the escape hatch for Directory Indexes and Update Logs."
|
|
66
|
+
),
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
def tool(**options: Any) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
|
|
70
|
+
"""Register a tool whose :class:`AgentToolError` reaches the client as a ``ToolError``.
|
|
71
|
+
|
|
72
|
+
MCP SDK >= 2.2 hides the text of any other exception from clients, so the
|
|
73
|
+
structured ``{"code", "message", "retry_with"}`` payload must travel as a ``ToolError``.
|
|
74
|
+
"""
|
|
75
|
+
register = server.tool(**options)
|
|
76
|
+
|
|
77
|
+
def decorate(fn: Callable[..., Any]) -> Callable[..., Any]:
|
|
78
|
+
@functools.wraps(fn)
|
|
79
|
+
def wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
80
|
+
try:
|
|
81
|
+
return fn(*args, **kwargs)
|
|
82
|
+
except AgentToolError as exc:
|
|
83
|
+
raise ToolError(str(exc)) from exc
|
|
84
|
+
|
|
85
|
+
return register(wrapper)
|
|
86
|
+
|
|
87
|
+
return decorate
|
|
88
|
+
|
|
89
|
+
@tool(
|
|
90
|
+
name="status",
|
|
91
|
+
description=(
|
|
92
|
+
"Report the running backend's Source Status. Returns the GET /api/source/status "
|
|
93
|
+
"envelope: source identity, git provenance, Read Index freshness, and coarse level."
|
|
94
|
+
),
|
|
95
|
+
)
|
|
96
|
+
def status() -> dict[str, Any]:
|
|
97
|
+
return tool_status(base_url, profile=profile)
|
|
98
|
+
|
|
99
|
+
@tool(
|
|
100
|
+
name="read_concept",
|
|
101
|
+
description=(
|
|
102
|
+
"Read a Concept by Concept ID. Emits the read envelope "
|
|
103
|
+
"(document_path, readable_status, concept_id, metadata, body, freshness). "
|
|
104
|
+
"Optional heading_fragment reads one Section. "
|
|
105
|
+
"Fails with a selector-error when the ID is missing or reserved; "
|
|
106
|
+
"retry reserved files via read_document with the file's document_path."
|
|
107
|
+
),
|
|
108
|
+
)
|
|
109
|
+
def read_concept(concept_id: str, heading_fragment: str | None = None) -> dict[str, Any]:
|
|
110
|
+
return tool_read_concept(base_url, concept_id, heading_fragment=heading_fragment, profile=profile)
|
|
111
|
+
|
|
112
|
+
@tool(
|
|
113
|
+
name="read_document",
|
|
114
|
+
description=(
|
|
115
|
+
"Read any readable file (Concept, Directory Index, or "
|
|
116
|
+
"Update Log) by exact bundle-relative document path. This is the escape "
|
|
117
|
+
"hatch for reserved files. Optional heading_fragment "
|
|
118
|
+
"reads one Section."
|
|
119
|
+
),
|
|
120
|
+
)
|
|
121
|
+
def read_document(document_path: str, heading_fragment: str | None = None) -> dict[str, Any]:
|
|
122
|
+
return tool_read_document(base_url, document_path, heading_fragment=heading_fragment, profile=profile)
|
|
123
|
+
|
|
124
|
+
@tool(
|
|
125
|
+
name="read_root",
|
|
126
|
+
description=(
|
|
127
|
+
"Read the bundle-root Directory Index (index.md at the Active Bundle root). "
|
|
128
|
+
"Optional heading_fragment reads one Section."
|
|
129
|
+
),
|
|
130
|
+
)
|
|
131
|
+
def read_root(heading_fragment: str | None = None) -> dict[str, Any]:
|
|
132
|
+
return tool_read_root(base_url, heading_fragment=heading_fragment, profile=profile)
|
|
133
|
+
|
|
134
|
+
@tool(
|
|
135
|
+
name="read_raw",
|
|
136
|
+
description=(
|
|
137
|
+
"Return exact Raw Content (source bytes as text) for one selector: exactly one "
|
|
138
|
+
"of concept_id, document_path, or root must be set. Use when the parsed body is "
|
|
139
|
+
"not enough (e.g. inspecting frontmatter or degraded documents)."
|
|
140
|
+
),
|
|
141
|
+
)
|
|
142
|
+
def read_raw(
|
|
143
|
+
concept_id: str | None = None,
|
|
144
|
+
document_path: str | None = None,
|
|
145
|
+
root: bool = False,
|
|
146
|
+
) -> str:
|
|
147
|
+
_require_exactly_one_selector(
|
|
148
|
+
[("concept_id", concept_id is not None), ("document_path", document_path is not None), ("root", root)],
|
|
149
|
+
command="read_raw",
|
|
150
|
+
)
|
|
151
|
+
return tool_read_raw(
|
|
152
|
+
base_url, concept_id=concept_id, document_path=document_path, root=root, profile=profile
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
@tool(
|
|
156
|
+
name="search",
|
|
157
|
+
description=(
|
|
158
|
+
"Search the Active Bundle via the running backend. Emits a single envelope "
|
|
159
|
+
"with echoed query/filters/pagination inputs, ranked results, has_more, total, "
|
|
160
|
+
"counts, and freshness. Free-text query: every word must match, the last also as a prefix; "
|
|
161
|
+
'"quoted phrases", -word to exclude; OR joins the words directly on either side of it.'
|
|
162
|
+
),
|
|
163
|
+
)
|
|
164
|
+
def search(
|
|
165
|
+
query: str,
|
|
166
|
+
limit: int | None = None,
|
|
167
|
+
offset: int | None = None,
|
|
168
|
+
type: str | None = None,
|
|
169
|
+
tags: str | None = None,
|
|
170
|
+
path_prefix: str | None = None,
|
|
171
|
+
trust_tier: str | None = None,
|
|
172
|
+
effective_status: str | None = None,
|
|
173
|
+
staleness: str | None = None,
|
|
174
|
+
source_resource: str | None = None,
|
|
175
|
+
runtime: str | None = None,
|
|
176
|
+
) -> dict[str, Any]:
|
|
177
|
+
return tool_search(
|
|
178
|
+
base_url,
|
|
179
|
+
query,
|
|
180
|
+
limit=limit,
|
|
181
|
+
offset=offset,
|
|
182
|
+
type_filter=type,
|
|
183
|
+
tags=tags,
|
|
184
|
+
path_prefix=path_prefix,
|
|
185
|
+
trust_tier=trust_tier,
|
|
186
|
+
effective_status=effective_status,
|
|
187
|
+
staleness=staleness,
|
|
188
|
+
source_resource=source_resource,
|
|
189
|
+
runtime=runtime,
|
|
190
|
+
profile=profile,
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
@tool(
|
|
194
|
+
name="list_concepts",
|
|
195
|
+
description=(
|
|
196
|
+
"List Concepts in the Active Bundle with navigation summary "
|
|
197
|
+
"fields (concept_id, document_path, title, type, description, tags, signals, runtime). "
|
|
198
|
+
"Optional shared list/search filters match GET /api/documents."
|
|
199
|
+
),
|
|
200
|
+
)
|
|
201
|
+
def list_concepts(
|
|
202
|
+
type: str | None = None,
|
|
203
|
+
tags: str | None = None,
|
|
204
|
+
path_prefix: str | None = None,
|
|
205
|
+
trust_tier: str | None = None,
|
|
206
|
+
effective_status: str | None = None,
|
|
207
|
+
staleness: str | None = None,
|
|
208
|
+
source_resource: str | None = None,
|
|
209
|
+
runtime: str | None = None,
|
|
210
|
+
) -> dict[str, Any]:
|
|
211
|
+
return tool_list_concepts(
|
|
212
|
+
base_url,
|
|
213
|
+
type_filter=type,
|
|
214
|
+
tags=tags,
|
|
215
|
+
path_prefix=path_prefix,
|
|
216
|
+
trust_tier=trust_tier,
|
|
217
|
+
effective_status=effective_status,
|
|
218
|
+
staleness=staleness,
|
|
219
|
+
source_resource=source_resource,
|
|
220
|
+
runtime=runtime,
|
|
221
|
+
profile=profile,
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
@tool(
|
|
225
|
+
name="list_readable",
|
|
226
|
+
description=(
|
|
227
|
+
"List readable Knowledge Bundle documents with hierarchy and selector "
|
|
228
|
+
"fields plus readable_status (concept, directory-index, "
|
|
229
|
+
"update-log). Emits the same DocumentListResponse contract as GET /api/documents. "
|
|
230
|
+
"Optional shared list/search filters match that route."
|
|
231
|
+
),
|
|
232
|
+
)
|
|
233
|
+
def list_readable(
|
|
234
|
+
type: str | None = None,
|
|
235
|
+
tags: str | None = None,
|
|
236
|
+
path_prefix: str | None = None,
|
|
237
|
+
trust_tier: str | None = None,
|
|
238
|
+
effective_status: str | None = None,
|
|
239
|
+
staleness: str | None = None,
|
|
240
|
+
source_resource: str | None = None,
|
|
241
|
+
runtime: str | None = None,
|
|
242
|
+
) -> dict[str, Any]:
|
|
243
|
+
return tool_list_readable(
|
|
244
|
+
base_url,
|
|
245
|
+
type_filter=type,
|
|
246
|
+
tags=tags,
|
|
247
|
+
path_prefix=path_prefix,
|
|
248
|
+
trust_tier=trust_tier,
|
|
249
|
+
effective_status=effective_status,
|
|
250
|
+
staleness=staleness,
|
|
251
|
+
source_resource=source_resource,
|
|
252
|
+
runtime=runtime,
|
|
253
|
+
profile=profile,
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
@tool(
|
|
257
|
+
name="diagnostics",
|
|
258
|
+
description=(
|
|
259
|
+
"Return Bundle Diagnostics for the Active Bundle via GET /api/diagnostics. "
|
|
260
|
+
"Emits freshness, severity summary, category counts, ordered diagnostics rows, "
|
|
261
|
+
"and unfiltered facets. Optional filters: severity, code, path_prefix, "
|
|
262
|
+
"projection_impact, category (repeatable list values where applicable)."
|
|
263
|
+
),
|
|
264
|
+
)
|
|
265
|
+
def diagnostics(
|
|
266
|
+
severity: list[str] | None = None,
|
|
267
|
+
code: list[str] | None = None,
|
|
268
|
+
path_prefix: str | None = None,
|
|
269
|
+
projection_impact: list[str] | None = None,
|
|
270
|
+
category: list[str] | None = None,
|
|
271
|
+
) -> dict[str, Any]:
|
|
272
|
+
return tool_diagnostics(
|
|
273
|
+
base_url,
|
|
274
|
+
severity=severity,
|
|
275
|
+
code=code,
|
|
276
|
+
path_prefix=path_prefix,
|
|
277
|
+
projection_impact=projection_impact,
|
|
278
|
+
category=category,
|
|
279
|
+
profile=profile,
|
|
280
|
+
)
|
|
281
|
+
|
|
282
|
+
@tool(
|
|
283
|
+
name="relationships",
|
|
284
|
+
description=(
|
|
285
|
+
"Return the RelationshipsResponse Shared Capability Contract for one document "
|
|
286
|
+
"(document label, generation, incoming/outgoing Relationship rows with origin and evidence). "
|
|
287
|
+
"Focus by strict Concept ID or by exact document_path (escape hatch for Concepts without a Concept Route "
|
|
288
|
+
"and reserved files). Parity: REST GET /api/relationships / CLI relationships."
|
|
289
|
+
),
|
|
290
|
+
)
|
|
291
|
+
def relationships(
|
|
292
|
+
concept_id: str | None = None,
|
|
293
|
+
document_path: str | None = None,
|
|
294
|
+
) -> dict[str, Any]:
|
|
295
|
+
_require_exactly_one_selector(
|
|
296
|
+
[("concept_id", concept_id is not None), ("document_path", document_path is not None)],
|
|
297
|
+
command="relationships",
|
|
298
|
+
)
|
|
299
|
+
return tool_relationships(base_url, concept_id=concept_id, document_path=document_path, profile=profile)
|
|
300
|
+
|
|
301
|
+
@tool(
|
|
302
|
+
name="wait_until_fresh",
|
|
303
|
+
description=(
|
|
304
|
+
"Block until the backend's Read Index is 'fresh' at or beyond a given generation. "
|
|
305
|
+
"Emits the Shared Capability Contract WaitUntilFreshResponse "
|
|
306
|
+
"(outcome/generation/state) from GET /api/freshness/wait. Event-driven "
|
|
307
|
+
"server-side; returns outcome='timeout' when the timeout elapses first."
|
|
308
|
+
),
|
|
309
|
+
)
|
|
310
|
+
def wait_until_fresh(timeout: float = 5.0, last_seen_generation: int = 0) -> dict[str, Any]:
|
|
311
|
+
return tool_wait_until_fresh(
|
|
312
|
+
base_url, timeout=timeout, last_seen_generation=last_seen_generation, profile=profile
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
return server
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def resolve_startup_target(argv: Sequence[str] | None = None) -> str:
|
|
319
|
+
"""Parse startup args and resolve the backend base URL, or raise :class:`AgentCliError`.
|
|
320
|
+
|
|
321
|
+
Args:
|
|
322
|
+
argv: Argument vector excluding the program name. ``None`` reads
|
|
323
|
+
from ``sys.argv[1:]``; tests pass an explicit list.
|
|
324
|
+
|
|
325
|
+
Returns:
|
|
326
|
+
The resolved base URL.
|
|
327
|
+
|
|
328
|
+
Raises:
|
|
329
|
+
AgentCliError: With ``EXIT_USAGE`` when target resolution fails
|
|
330
|
+
(unknown profile or no target at all).
|
|
331
|
+
"""
|
|
332
|
+
args = _startup_args(argv)
|
|
333
|
+
return resolve_base_url(args.base_url, profile=args.profile)
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def _startup_args(argv: Sequence[str] | None) -> argparse.Namespace:
|
|
337
|
+
"""Parse the ``--base-url`` and ``--profile`` startup arguments."""
|
|
338
|
+
parser = argparse.ArgumentParser(
|
|
339
|
+
prog=PROG_NAME,
|
|
340
|
+
description=(
|
|
341
|
+
"Backend-connected Agent MCP adapter. Resolves the target backend at startup "
|
|
342
|
+
"(explicit --base-url, then KLEIA_AGENT_BASE_URL, then --profile, then default "
|
|
343
|
+
"profile) and serves the CLI's consumption capabilities as MCP tools over "
|
|
344
|
+
"stdio."
|
|
345
|
+
),
|
|
346
|
+
)
|
|
347
|
+
parser.add_argument(
|
|
348
|
+
"--base-url",
|
|
349
|
+
default=None,
|
|
350
|
+
metavar="URL",
|
|
351
|
+
help=(
|
|
352
|
+
"Base URL of the running backend. Falls back to KLEIA_AGENT_BASE_URL, then to "
|
|
353
|
+
"the named/default profile. No localhost fallback."
|
|
354
|
+
),
|
|
355
|
+
)
|
|
356
|
+
parser.add_argument(
|
|
357
|
+
"--profile",
|
|
358
|
+
default=None,
|
|
359
|
+
metavar="NAME",
|
|
360
|
+
help="Named profile to resolve the backend from (used after --base-url and env var).",
|
|
361
|
+
)
|
|
362
|
+
return parser.parse_args(list(argv) if argv is not None else None)
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
366
|
+
"""Console-script entrypoint: resolve target, build server, run stdio.
|
|
367
|
+
|
|
368
|
+
On target-resolution failure emits a JSON error envelope to stderr
|
|
369
|
+
and returns :data:`kleia.errors.EXIT_USAGE` (2). Success
|
|
370
|
+
normally does not return - the stdio server blocks until the client
|
|
371
|
+
disconnects, then returns 0.
|
|
372
|
+
"""
|
|
373
|
+
try:
|
|
374
|
+
args = _startup_args(argv)
|
|
375
|
+
base_url = resolve_base_url(args.base_url, profile=args.profile)
|
|
376
|
+
except AgentCliError as exc:
|
|
377
|
+
emit_error(exc.code, exc.message)
|
|
378
|
+
return exc.exit_code
|
|
379
|
+
server = build_server(base_url, args.profile)
|
|
380
|
+
server.run(transport="stdio")
|
|
381
|
+
return 0
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def _require_exactly_one_selector(
|
|
385
|
+
selectors: list[tuple[str, bool]], *, command: str
|
|
386
|
+
) -> None:
|
|
387
|
+
"""Enforce the ``read``/``relationships`` "exactly one selector" contract on MCP tool args."""
|
|
388
|
+
chosen = [name for name, on in selectors if on]
|
|
389
|
+
if len(chosen) == 0:
|
|
390
|
+
names = ", ".join(name for name, _ in selectors)
|
|
391
|
+
raise AgentToolError(
|
|
392
|
+
code=ErrorCode.USAGE,
|
|
393
|
+
message=f"{command} requires exactly one of {names}.",
|
|
394
|
+
)
|
|
395
|
+
if len(chosen) > 1:
|
|
396
|
+
raise AgentToolError(
|
|
397
|
+
code=ErrorCode.USAGE,
|
|
398
|
+
message=f"{command} selectors are mutually exclusive; got {', '.join(chosen)}.",
|
|
399
|
+
)
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
if __name__ == "__main__": # pragma: no cover
|
|
403
|
+
sys.exit(main())
|
kleia/agent_read.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""Formal Agent CLI ``read`` command.
|
|
2
|
+
|
|
3
|
+
Reads OKF documents from a running Kleia backend. Selector semantics
|
|
4
|
+
are strict per ADR 0006: ``--concept-id`` succeeds for every Concept,
|
|
5
|
+
``--document-path`` is the explicit escape hatch for Directory Indexes
|
|
6
|
+
and Update Logs, and ``--root`` targets the bundle-root Directory Index.
|
|
7
|
+
|
|
8
|
+
Returns the complete-read envelope dict, or exact Raw Content when
|
|
9
|
+
``raw`` is true. The Typer layer presents AXI TOON / ``--json`` / ``--raw``.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from kleia import rest
|
|
17
|
+
from kleia.target_resolver import resolve_base_url
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def run_read(
|
|
21
|
+
*,
|
|
22
|
+
concept_id: str | None,
|
|
23
|
+
document_path: str | None,
|
|
24
|
+
root: bool,
|
|
25
|
+
raw: bool,
|
|
26
|
+
heading_fragment: str | None = None,
|
|
27
|
+
base_url: str | None,
|
|
28
|
+
profile: str | None = None,
|
|
29
|
+
) -> dict[str, Any] | str:
|
|
30
|
+
"""Execute the formal ``read`` command.
|
|
31
|
+
|
|
32
|
+
Exactly one of ``concept_id``, ``document_path``, or ``root`` must be
|
|
33
|
+
truthy; the Typer layer enforces this before calling in.
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
Exact Raw Content when ``raw`` is true, otherwise the complete-read
|
|
37
|
+
envelope dict.
|
|
38
|
+
"""
|
|
39
|
+
resolved_url = resolve_base_url(base_url, profile=profile)
|
|
40
|
+
try:
|
|
41
|
+
if raw:
|
|
42
|
+
return rest.read_raw(
|
|
43
|
+
resolved_url,
|
|
44
|
+
concept_id=concept_id,
|
|
45
|
+
document_path=document_path,
|
|
46
|
+
root=root,
|
|
47
|
+
profile=profile,
|
|
48
|
+
)
|
|
49
|
+
if root:
|
|
50
|
+
return rest.read_root(resolved_url, heading_fragment=heading_fragment, profile=profile)
|
|
51
|
+
if concept_id is not None:
|
|
52
|
+
return rest.read_concept(resolved_url, concept_id, heading_fragment=heading_fragment, profile=profile)
|
|
53
|
+
assert document_path is not None
|
|
54
|
+
return rest.read_document(resolved_url, document_path, heading_fragment=heading_fragment, profile=profile)
|
|
55
|
+
except rest.RestError as exc:
|
|
56
|
+
rest.raise_as_cli(exc, command="read", base_url=base_url, profile=profile)
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""Formal Agent CLI ``relationships`` command.
|
|
2
|
+
|
|
3
|
+
Wraps the shared REST relationships operation over
|
|
4
|
+
``GET /api/relationships/{document_path}``.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from kleia import rest
|
|
12
|
+
from kleia.target_resolver import resolve_base_url
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def run_relationships(
|
|
16
|
+
*,
|
|
17
|
+
concept_id: str | None,
|
|
18
|
+
document_path: str | None,
|
|
19
|
+
base_url: str | None,
|
|
20
|
+
profile: str | None = None,
|
|
21
|
+
) -> dict[str, Any]:
|
|
22
|
+
"""Execute the formal ``relationships`` command and return the shared dict.
|
|
23
|
+
|
|
24
|
+
Exactly one of ``concept_id`` or ``document_path`` must be truthy; the
|
|
25
|
+
Typer layer enforces this before calling in.
|
|
26
|
+
"""
|
|
27
|
+
resolved_url = resolve_base_url(base_url, profile=profile)
|
|
28
|
+
try:
|
|
29
|
+
return rest.relationships(
|
|
30
|
+
resolved_url, concept_id=concept_id, document_path=document_path, profile=profile
|
|
31
|
+
)
|
|
32
|
+
except rest.RestError as exc:
|
|
33
|
+
rest.raise_as_cli(exc, command="relationships", base_url=base_url, profile=profile)
|
kleia/agent_search.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""Formal Agent CLI ``search`` command.
|
|
2
|
+
|
|
3
|
+
Wraps the shared REST search operation over ``GET /api/search``.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from kleia import rest
|
|
11
|
+
from kleia.target_resolver import resolve_base_url
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def run_search(
|
|
15
|
+
*,
|
|
16
|
+
query: str,
|
|
17
|
+
base_url: str | None,
|
|
18
|
+
profile: str | None = None,
|
|
19
|
+
limit: int | None = None,
|
|
20
|
+
offset: int | None = None,
|
|
21
|
+
type_filter: str | None = None,
|
|
22
|
+
tags: str | None = None,
|
|
23
|
+
path_prefix: str | None = None,
|
|
24
|
+
trust_tier: str | None = None,
|
|
25
|
+
effective_status: str | None = None,
|
|
26
|
+
staleness: str | None = None,
|
|
27
|
+
source_resource: str | None = None,
|
|
28
|
+
runtime: str | None = None,
|
|
29
|
+
) -> dict[str, Any]:
|
|
30
|
+
"""Execute the formal ``search`` command and return the shared dict."""
|
|
31
|
+
resolved_url = resolve_base_url(base_url, profile=profile)
|
|
32
|
+
try:
|
|
33
|
+
return rest.search(
|
|
34
|
+
resolved_url,
|
|
35
|
+
query,
|
|
36
|
+
limit=limit,
|
|
37
|
+
offset=offset,
|
|
38
|
+
type_filter=type_filter,
|
|
39
|
+
tags=tags,
|
|
40
|
+
path_prefix=path_prefix,
|
|
41
|
+
trust_tier=trust_tier,
|
|
42
|
+
effective_status=effective_status,
|
|
43
|
+
staleness=staleness,
|
|
44
|
+
source_resource=source_resource,
|
|
45
|
+
runtime=runtime,
|
|
46
|
+
profile=profile,
|
|
47
|
+
)
|
|
48
|
+
except rest.RestError as exc:
|
|
49
|
+
rest.raise_as_cli(exc, command="search", base_url=base_url, profile=profile)
|