driverkit 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.
- driverkit/__init__.py +93 -0
- driverkit/asgi.py +277 -0
- driverkit/auth.py +508 -0
- driverkit/client.py +413 -0
- driverkit/fastmcp_server.py +374 -0
- driverkit/mcp_core.py +988 -0
- driverkit/mcp_server.py +262 -0
- driverkit/relay.py +1531 -0
- driverkit/render.py +486 -0
- driverkit/serve_relay.py +670 -0
- driverkit/types.py +943 -0
- driverkit-0.1.0.dist-info/METADATA +11 -0
- driverkit-0.1.0.dist-info/RECORD +15 -0
- driverkit-0.1.0.dist-info/WHEEL +4 -0
- driverkit-0.1.0.dist-info/entry_points.txt +4 -0
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
"""
|
|
2
|
+
An authenticated MCP server over one driverkit tab, built on FastMCP.
|
|
3
|
+
|
|
4
|
+
*** THIS MODULE HAS NEVER BEEN EXECUTED. ***
|
|
5
|
+
|
|
6
|
+
``uv`` panics in macOS SystemConfiguration on the machine this was written on
|
|
7
|
+
and DNS was unreliable, so ``fastmcp`` could not be installed and nothing here
|
|
8
|
+
has run even once. It is written against the real FastMCP 2.x API from
|
|
9
|
+
knowledge of it, and it is the half of this package a human must verify before
|
|
10
|
+
trusting. :mod:`driverkit.mcp_server` is the server that runs today, has no
|
|
11
|
+
dependencies, and is tested; nothing here replaces it or is imported by it.
|
|
12
|
+
|
|
13
|
+
What a human must check, after ``uv add 'driverkit[fastmcp]'``:
|
|
14
|
+
|
|
15
|
+
1. ``from fastmcp.server.auth.auth import TokenVerifier`` and the
|
|
16
|
+
``AccessToken`` import below both resolve, and ``AccessToken``'s field names
|
|
17
|
+
are the ones used in :meth:`TabTokenVerifier.verify_token`. These have moved
|
|
18
|
+
between FastMCP versions more than once.
|
|
19
|
+
2. Subclassing ``fastmcp.tools.tool.Tool`` with an explicit ``parameters``
|
|
20
|
+
JSON Schema and an ``async def run`` is still how a tool with a
|
|
21
|
+
hand-written schema is registered. If FastMCP now wants
|
|
22
|
+
``Tool.from_function`` only, the fix is to build a function per verb and
|
|
23
|
+
attach the schema — the schema itself comes from
|
|
24
|
+
:func:`driverkit.mcp_core.tools_for` either way and does not change.
|
|
25
|
+
3. ``get_access_token()`` returns the verified token inside a tool call, and
|
|
26
|
+
``.token`` is the raw credential string. The pass-through in
|
|
27
|
+
:meth:`_VerbTool.run` is the whole security model of this file.
|
|
28
|
+
4. ``mcp.run(transport="http", ...)`` serves at the path and with the auth
|
|
29
|
+
challenge a client expects, and an unauthenticated request is refused with
|
|
30
|
+
a 401 rather than reaching a tool.
|
|
31
|
+
5. That an expired token is refused, and that the refusal reaches the client as
|
|
32
|
+
an auth error rather than as a tool result.
|
|
33
|
+
|
|
34
|
+
WHY FASTMCP'S AUTH AND NOT A SCHEME OF OUR OWN
|
|
35
|
+
----------------------------------------------
|
|
36
|
+
|
|
37
|
+
Stdio needs no authentication: the client launched the process, so the trust
|
|
38
|
+
boundary is the operating system's. An MCP server reachable over HTTP is a
|
|
39
|
+
different thing — it is an agent-facing endpoint that can drive a person's
|
|
40
|
+
logged-in browser — and it needs real authentication. FastMCP has that:
|
|
41
|
+
``TokenVerifier``, the ``auth=`` parameter, the ``get_access_token()``
|
|
42
|
+
dependency, a 401 with a challenge, and OAuth resource-server behaviour on top.
|
|
43
|
+
Hand-rolling any of it would mean an HTTP authentication scheme nobody else
|
|
44
|
+
implements and therefore nobody else audits, which is the opposite of what a
|
|
45
|
+
security-sensitive surface wants.
|
|
46
|
+
|
|
47
|
+
What is plugged into it is driverkit's own token format, as a custom
|
|
48
|
+
``TokenVerifier``. FastMCP ships a ``JWTVerifier``, and using it would mean the
|
|
49
|
+
tokens were JWTs — which would mean the *relay* verified JWTs, which would mean
|
|
50
|
+
``PyJWT`` or ``cryptography`` in the relay. The relay is stdlib-only on purpose
|
|
51
|
+
(PROTOCOL.md's whole argument is that it belongs inside a server you already
|
|
52
|
+
run, in any language, with no dependencies), so the format is HMAC-SHA256 with
|
|
53
|
+
a fixed algorithm prefix and the verifier here is twenty lines over
|
|
54
|
+
:meth:`driverkit.auth.TabAuth.principal`. One token format, two consumers.
|
|
55
|
+
|
|
56
|
+
THE SERVER HOLDS NO CREDENTIAL OF ITS OWN FOR THE CALLER'S WORK
|
|
57
|
+
---------------------------------------------------------------
|
|
58
|
+
|
|
59
|
+
The important property, and the one to preserve through any change above: the
|
|
60
|
+
token the agent authenticated with is the token this server presents to the
|
|
61
|
+
relay. It does not mint, swap or widen anything. So this server cannot reach a
|
|
62
|
+
tab the caller could not reach by talking to the relay directly, and a bug in
|
|
63
|
+
it cannot become a privilege escalation — there is no privilege here to
|
|
64
|
+
escalate to. A proxy that held an operator's credential and used it on behalf
|
|
65
|
+
of whoever authenticated would be exactly the ambient-authority component that
|
|
66
|
+
PROTOCOL.md's "the relay grants no authority" exists to keep out of the design.
|
|
67
|
+
|
|
68
|
+
The one exception is the MANIFEST, fetched once at startup with the operator's
|
|
69
|
+
own ``DK_TOKEN`` so the tool list exists before anybody connects. That is the
|
|
70
|
+
app's verb table — the static half, identical for every user, describing what
|
|
71
|
+
the app can do and nothing about anybody's screen. No snapshot, no tab listing
|
|
72
|
+
and no call is ever made with that credential.
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
from __future__ import annotations
|
|
76
|
+
|
|
77
|
+
import argparse
|
|
78
|
+
import os
|
|
79
|
+
import sys
|
|
80
|
+
from typing import Any
|
|
81
|
+
|
|
82
|
+
from .auth import TabAuth
|
|
83
|
+
from .client import Driverkit, DriverkitError
|
|
84
|
+
from .mcp_core import TabTools, instructions_for, pick_tab, tools_for
|
|
85
|
+
|
|
86
|
+
__all__ = ["FASTMCP_AVAILABLE", "TabTokenVerifier", "build_server", "main"]
|
|
87
|
+
|
|
88
|
+
#: Whether ``fastmcp`` imported. The module itself always imports, so that
|
|
89
|
+
#: ``driverkit`` is usable — and testable — on a machine where ``fastmcp`` is
|
|
90
|
+
#: not installed, which is the machine this was written on. Every entry point
|
|
91
|
+
#: below raises a message that says what to install.
|
|
92
|
+
FASTMCP_AVAILABLE = True
|
|
93
|
+
|
|
94
|
+
try: # pragma: no cover - depends on an optional dependency
|
|
95
|
+
from fastmcp import FastMCP
|
|
96
|
+
from fastmcp.exceptions import ToolError
|
|
97
|
+
from fastmcp.server.auth.auth import TokenVerifier
|
|
98
|
+
from fastmcp.server.dependencies import get_access_token
|
|
99
|
+
from fastmcp.tools.tool import Tool, ToolResult
|
|
100
|
+
from mcp.server.auth.provider import AccessToken
|
|
101
|
+
except Exception: # noqa: BLE001 - ImportError, and anything a broken install raises
|
|
102
|
+
FASTMCP_AVAILABLE = False
|
|
103
|
+
|
|
104
|
+
# Placeholders so the class definitions below are syntactically reachable.
|
|
105
|
+
# They are never instantiated: `_require_fastmcp` is called first by every
|
|
106
|
+
# path that would.
|
|
107
|
+
class _Missing:
|
|
108
|
+
def __init__(self, *_args: Any, **_kwargs: Any) -> None:
|
|
109
|
+
_require_fastmcp()
|
|
110
|
+
|
|
111
|
+
FastMCP = _Missing # type: ignore[assignment,misc]
|
|
112
|
+
Tool = _Missing # type: ignore[assignment,misc]
|
|
113
|
+
ToolResult = _Missing # type: ignore[assignment,misc]
|
|
114
|
+
ToolError = RuntimeError # type: ignore[assignment,misc]
|
|
115
|
+
TokenVerifier = _Missing # type: ignore[assignment,misc]
|
|
116
|
+
AccessToken = _Missing # type: ignore[assignment,misc]
|
|
117
|
+
|
|
118
|
+
def get_access_token() -> Any: # type: ignore[misc]
|
|
119
|
+
_require_fastmcp()
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
_INSTALL_HINT = (
|
|
123
|
+
"driverkit: this server needs `fastmcp`, which is an optional extra:\n"
|
|
124
|
+
" uv add 'driverkit[fastmcp]'\n"
|
|
125
|
+
"The stdio server in driverkit.mcp_server needs nothing and is the one that "
|
|
126
|
+
"runs today: `driverkit-mcp --tab <id>`."
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _require_fastmcp() -> None:
|
|
131
|
+
if not FASTMCP_AVAILABLE:
|
|
132
|
+
raise RuntimeError(_INSTALL_HINT)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
class TabTokenVerifier(TokenVerifier): # type: ignore[misc]
|
|
136
|
+
"""FastMCP's ``TokenVerifier`` over a driverkit tab token.
|
|
137
|
+
|
|
138
|
+
This is the whole of the authentication: a bearer token arrives, FastMCP
|
|
139
|
+
hands it here, and :meth:`driverkit.auth.TabAuth.principal` says whose it
|
|
140
|
+
is or refuses it. The same ``TabAuth`` the relay has, with the same keys and
|
|
141
|
+
the same audience, so there is one credential an agent needs and not two.
|
|
142
|
+
|
|
143
|
+
Returning ``None`` is how FastMCP is told to refuse. Every way a credential
|
|
144
|
+
can fail returns the same ``None`` with nothing logged, for the reason
|
|
145
|
+
:class:`driverkit.auth.AuthError` gives: the distinctions a caller might
|
|
146
|
+
want are the distinctions an attacker wants.
|
|
147
|
+
|
|
148
|
+
``client_id`` carries the principal. It is the field FastMCP surfaces to a
|
|
149
|
+
tool, and the principal is the only thing a tool here needs to know.
|
|
150
|
+
"""
|
|
151
|
+
|
|
152
|
+
def __init__(self, auth: TabAuth) -> None:
|
|
153
|
+
super().__init__()
|
|
154
|
+
self._auth = auth
|
|
155
|
+
|
|
156
|
+
async def verify_token(self, token: str) -> "AccessToken | None":
|
|
157
|
+
try:
|
|
158
|
+
principal = self._auth.principal(token)
|
|
159
|
+
except Exception: # noqa: BLE001 - AuthError, and never distinguished
|
|
160
|
+
return None
|
|
161
|
+
return AccessToken(
|
|
162
|
+
token=token,
|
|
163
|
+
client_id=principal.id,
|
|
164
|
+
# No scopes, and that absence is the design rather than an omission.
|
|
165
|
+
# A scope would be an authority this server granted, and it grants
|
|
166
|
+
# none: what the caller may do is what their own browser session may
|
|
167
|
+
# do, which the app checks and nothing here can widen. See
|
|
168
|
+
# PROTOCOL.md, "Who is allowed to do this".
|
|
169
|
+
scopes=[],
|
|
170
|
+
expires_at=None,
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class _VerbTool(Tool): # type: ignore[misc]
|
|
175
|
+
"""One manifest verb as one FastMCP tool.
|
|
176
|
+
|
|
177
|
+
The schema is :func:`driverkit.mcp_core.tools_for`'s, unchanged, because
|
|
178
|
+
that function is the only description of the app's verbs that exists and a
|
|
179
|
+
second translation here would be a second one to get wrong.
|
|
180
|
+
|
|
181
|
+
Each call builds its own :class:`driverkit.Driverkit` from the CALLER'S
|
|
182
|
+
token. Not a client held on the server: a shared client would hold one
|
|
183
|
+
credential and serve every authenticated caller with it, which is the
|
|
184
|
+
confused-deputy shape this file's docstring is about. One client per call
|
|
185
|
+
costs a connection and buys the property that this server cannot reach what
|
|
186
|
+
its caller cannot.
|
|
187
|
+
"""
|
|
188
|
+
|
|
189
|
+
relay_url: str
|
|
190
|
+
tab: str | None
|
|
191
|
+
verb: str
|
|
192
|
+
|
|
193
|
+
async def run(self, arguments: dict[str, Any]) -> "ToolResult":
|
|
194
|
+
access = get_access_token()
|
|
195
|
+
if access is None: # pragma: no cover - FastMCP refuses before this
|
|
196
|
+
raise ToolError("unauthenticated")
|
|
197
|
+
|
|
198
|
+
client = Driverkit(self.relay_url, token=access.token)
|
|
199
|
+
try:
|
|
200
|
+
tab = self.tab or _only_tab(client)
|
|
201
|
+
outcome = TabTools(client, tab).call(self.verb, arguments)
|
|
202
|
+
except DriverkitError as err:
|
|
203
|
+
# A transport failure, not a tool result: nothing ran, so there is
|
|
204
|
+
# no outcome to report to the model. `ToolError` is FastMCP's
|
|
205
|
+
# `isError`, which is what that means.
|
|
206
|
+
raise ToolError(f"driverkit: {err}") from err
|
|
207
|
+
|
|
208
|
+
if outcome.is_error:
|
|
209
|
+
# True only when the call provably never landed. A refusal and a
|
|
210
|
+
# `timeout` both come back as ordinary text — see
|
|
211
|
+
# `TabTools.call`, where the reasoning lives for both servers.
|
|
212
|
+
raise ToolError(outcome.text)
|
|
213
|
+
return ToolResult(content=outcome.text)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def _only_tab(client: Driverkit) -> str:
|
|
217
|
+
"""The caller's one tab, or an error naming the others.
|
|
218
|
+
|
|
219
|
+
Reached only when the server was started without ``--tab`` and the caller's
|
|
220
|
+
token binds none. The listing is already scoped to the caller's principal by
|
|
221
|
+
the relay, so "the only tab" means "the only one of theirs" — two people
|
|
222
|
+
driving two tabs each both get their own, and neither sees the other's in
|
|
223
|
+
the error message.
|
|
224
|
+
"""
|
|
225
|
+
tabs = client.tabs()
|
|
226
|
+
if not tabs:
|
|
227
|
+
raise ToolError("driverkit: no tab of yours is connected to this relay")
|
|
228
|
+
# The same rule `driverkit-mcp` applies, from the same function, so a person
|
|
229
|
+
# with two tabs open gets the one they are looking at whichever server they
|
|
230
|
+
# are going through. See `pick_tab` for the four cases, including the ones
|
|
231
|
+
# where it declines to answer — and note that it declines here too rather
|
|
232
|
+
# than falling back to the first tab.
|
|
233
|
+
chosen = pick_tab(tabs)
|
|
234
|
+
if chosen is not None:
|
|
235
|
+
return chosen.id
|
|
236
|
+
# Guessing would mean driving somebody's other screen — and here "somebody"
|
|
237
|
+
# may be the same person, which is no better.
|
|
238
|
+
# The focus each tab reported is named beside its id, including when it
|
|
239
|
+
# reported none: "unknown" is why this could not be answered, and an
|
|
240
|
+
# operator who cannot see that will think the feature is broken rather than
|
|
241
|
+
# that their page is not reporting.
|
|
242
|
+
listed = ", ".join(f"{t.id} (focus: {t.focus or 'unknown'})" for t in tabs)
|
|
243
|
+
raise ToolError(
|
|
244
|
+
"driverkit: this server drives one tab and cannot tell which of your "
|
|
245
|
+
f"{len(tabs)} connected tabs you mean: {listed}. Use a tab-bound token, or run a "
|
|
246
|
+
"server per tab."
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def build_server(
|
|
251
|
+
*,
|
|
252
|
+
relay_url: str,
|
|
253
|
+
auth: TabAuth,
|
|
254
|
+
tab: str | None = None,
|
|
255
|
+
bootstrap_token: str | None = None,
|
|
256
|
+
) -> "FastMCP":
|
|
257
|
+
"""A FastMCP server whose tools are this app's verbs.
|
|
258
|
+
|
|
259
|
+
``bootstrap_token`` fetches the MANIFEST once, at startup, so a tool list
|
|
260
|
+
exists before any agent connects. It is the operator's own credential and
|
|
261
|
+
it is used for nothing else — see the module docstring: the manifest is the
|
|
262
|
+
app's verb table, identical for every user, and carries nothing about
|
|
263
|
+
anybody's screen. Every snapshot, every tab listing and every call uses the
|
|
264
|
+
caller's own token.
|
|
265
|
+
|
|
266
|
+
``tab`` fixes which tab this server drives. Omit it and each call resolves
|
|
267
|
+
the caller's single tab, which fails usefully when they have two. A
|
|
268
|
+
tab-bound token is the better answer where the agent knows its tab: the
|
|
269
|
+
relay then refuses anything else as ``no-such-tab`` and this server is not
|
|
270
|
+
the thing enforcing it.
|
|
271
|
+
"""
|
|
272
|
+
_require_fastmcp()
|
|
273
|
+
|
|
274
|
+
manifest = Driverkit(relay_url, token=bootstrap_token).manifest(tab) if tab else None
|
|
275
|
+
if manifest is None:
|
|
276
|
+
# Without a tab there is nothing to ask for a manifest, so the operator
|
|
277
|
+
# must say which tab this server is for. Deferring would mean a server
|
|
278
|
+
# that advertises no tools until somebody calls one, which no client
|
|
279
|
+
# recovers from.
|
|
280
|
+
raise ValueError(
|
|
281
|
+
"driverkit: build_server needs a tab to read the manifest from. The manifest is "
|
|
282
|
+
"the app's static verb table; a server cannot advertise tools without it."
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
mcp = FastMCP(
|
|
286
|
+
name=f"driverkit/{manifest.app}",
|
|
287
|
+
instructions=instructions_for(manifest),
|
|
288
|
+
auth=TabTokenVerifier(auth),
|
|
289
|
+
)
|
|
290
|
+
|
|
291
|
+
for schema in tools_for(manifest):
|
|
292
|
+
mcp.add_tool(
|
|
293
|
+
_VerbTool(
|
|
294
|
+
name=schema["name"],
|
|
295
|
+
description=schema["description"],
|
|
296
|
+
parameters=schema["inputSchema"],
|
|
297
|
+
relay_url=relay_url,
|
|
298
|
+
tab=tab,
|
|
299
|
+
verb=schema["name"],
|
|
300
|
+
)
|
|
301
|
+
)
|
|
302
|
+
return mcp
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def main(argv: list[str] | None = None) -> int: # pragma: no cover - unexecuted
|
|
306
|
+
"""The ``driverkit-fastmcp`` console script.
|
|
307
|
+
|
|
308
|
+
Not wired into ``[project.scripts]`` as the default anything. ``uv add
|
|
309
|
+
driverkit`` installs ``driverkit-mcp``, which works; this appears only when
|
|
310
|
+
the ``fastmcp`` extra is installed, and a run without it fails with the
|
|
311
|
+
install hint rather than a traceback about a missing module.
|
|
312
|
+
"""
|
|
313
|
+
parser = argparse.ArgumentParser(
|
|
314
|
+
prog="driverkit-fastmcp",
|
|
315
|
+
description="Expose one driverkit tab to authenticated MCP clients over HTTP.",
|
|
316
|
+
epilog="UNVERIFIED: see this module's docstring for what to check first.",
|
|
317
|
+
)
|
|
318
|
+
parser.add_argument("--relay", default=os.environ.get("DK_RELAY", "http://127.0.0.1:7777"))
|
|
319
|
+
parser.add_argument("--tab", default=os.environ.get("DK_TAB"), help="the tab to drive")
|
|
320
|
+
parser.add_argument(
|
|
321
|
+
"--audience",
|
|
322
|
+
default=os.environ.get("DK_AUDIENCE"),
|
|
323
|
+
help="the audience of the tokens this server accepts (env DK_AUDIENCE). The same "
|
|
324
|
+
"value the relay is configured with: one token, two consumers.",
|
|
325
|
+
)
|
|
326
|
+
parser.add_argument("--host", default=os.environ.get("DK_MCP_HOST", "127.0.0.1"))
|
|
327
|
+
parser.add_argument("--port", type=int, default=int(os.environ.get("DK_MCP_PORT", "7788")))
|
|
328
|
+
parser.add_argument(
|
|
329
|
+
"--stdio",
|
|
330
|
+
action="store_true",
|
|
331
|
+
help="serve over stdio instead of HTTP. Authentication is then the operating "
|
|
332
|
+
"system's, as it is for driverkit-mcp.",
|
|
333
|
+
)
|
|
334
|
+
args = parser.parse_args(argv)
|
|
335
|
+
|
|
336
|
+
if not FASTMCP_AVAILABLE:
|
|
337
|
+
print(_INSTALL_HINT, file=sys.stderr)
|
|
338
|
+
return 2
|
|
339
|
+
|
|
340
|
+
secret = os.environ.get("DK_SECRET")
|
|
341
|
+
if not secret or not args.audience:
|
|
342
|
+
# env, not an argument: an argument is visible in `ps` to every user on
|
|
343
|
+
# the box.
|
|
344
|
+
print(
|
|
345
|
+
"driverkit-fastmcp: set DK_SECRET and --audience/DK_AUDIENCE to the same values "
|
|
346
|
+
"the relay has. Without them this server would accept any caller, and it is "
|
|
347
|
+
"reachable over HTTP.",
|
|
348
|
+
file=sys.stderr,
|
|
349
|
+
)
|
|
350
|
+
return 2
|
|
351
|
+
if not args.tab:
|
|
352
|
+
print("driverkit-fastmcp: --tab is required; see build_server.", file=sys.stderr)
|
|
353
|
+
return 2
|
|
354
|
+
|
|
355
|
+
try:
|
|
356
|
+
mcp = build_server(
|
|
357
|
+
relay_url=args.relay,
|
|
358
|
+
auth=TabAuth(secret=secret, audience=args.audience),
|
|
359
|
+
tab=args.tab,
|
|
360
|
+
bootstrap_token=os.environ.get("DK_TOKEN"),
|
|
361
|
+
)
|
|
362
|
+
except (ValueError, DriverkitError) as err:
|
|
363
|
+
print(f"driverkit-fastmcp: {err}", file=sys.stderr)
|
|
364
|
+
return 2
|
|
365
|
+
|
|
366
|
+
if args.stdio:
|
|
367
|
+
mcp.run()
|
|
368
|
+
else:
|
|
369
|
+
mcp.run(transport="http", host=args.host, port=args.port)
|
|
370
|
+
return 0
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
if __name__ == "__main__": # pragma: no cover
|
|
374
|
+
raise SystemExit(main())
|