mcp-dispatch 0.1.1__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.
- mcp_dispatch/__init__.py +59 -0
- mcp_dispatch/__main__.py +3 -0
- mcp_dispatch/agent_instructions/host.md +102 -0
- mcp_dispatch/agent_instructions/plugin.md +222 -0
- mcp_dispatch/check.py +310 -0
- mcp_dispatch/cli.py +276 -0
- mcp_dispatch/command.py +121 -0
- mcp_dispatch/dispatch.py +208 -0
- mcp_dispatch/docs.py +100 -0
- mcp_dispatch/errors.py +32 -0
- mcp_dispatch/lenient.py +172 -0
- mcp_dispatch/mcp_tools.py +195 -0
- mcp_dispatch/py.typed +0 -0
- mcp_dispatch/registry.py +972 -0
- mcp_dispatch/skills.py +533 -0
- mcp_dispatch/tags.py +173 -0
- mcp_dispatch/trials.py +380 -0
- mcp_dispatch-0.1.1.dist-info/METADATA +86 -0
- mcp_dispatch-0.1.1.dist-info/RECORD +22 -0
- mcp_dispatch-0.1.1.dist-info/WHEEL +4 -0
- mcp_dispatch-0.1.1.dist-info/entry_points.txt +2 -0
- mcp_dispatch-0.1.1.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"""A registry's tools, as an MCP server shows them.
|
|
2
|
+
|
|
3
|
+
Each of the host's tools takes one JSON object: ``path`` plus the action's
|
|
4
|
+
arguments as flat keys, or ``calls`` with a list of such objects. The schema
|
|
5
|
+
therefore declares those two and lets every other key through; the dispatcher
|
|
6
|
+
checks the rest against the action's signature. The ``search`` tool declares
|
|
7
|
+
``q`` and ``t`` in place of ``path``. The tools differ in what they may
|
|
8
|
+
change, so a client's per-tool permission is a per-risk switch.
|
|
9
|
+
|
|
10
|
+
The descriptions are the host's (``Tool.description``): they are read by an
|
|
11
|
+
assistant before it acts.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
from collections.abc import Awaitable, Callable, Mapping
|
|
17
|
+
from typing import TYPE_CHECKING, Any
|
|
18
|
+
|
|
19
|
+
from mcp_dispatch.errors import DispatchError
|
|
20
|
+
from mcp_dispatch.registry import (
|
|
21
|
+
CALLS_KEY,
|
|
22
|
+
KIND_KEY,
|
|
23
|
+
MAX_CALLS,
|
|
24
|
+
PATH_KEY,
|
|
25
|
+
Bound,
|
|
26
|
+
Help,
|
|
27
|
+
Registry,
|
|
28
|
+
label,
|
|
29
|
+
repaired,
|
|
30
|
+
report,
|
|
31
|
+
)
|
|
32
|
+
from mcp_dispatch.tags import SEARCH, Tool
|
|
33
|
+
|
|
34
|
+
if TYPE_CHECKING:
|
|
35
|
+
from mcp.server.fastmcp import FastMCP
|
|
36
|
+
from mcp.types import ToolAnnotations
|
|
37
|
+
|
|
38
|
+
#: What the host does with one checked call: ``run(ctx, bound)`` answers with
|
|
39
|
+
#: its text. The host opens its transaction (``bound.tool.writes`` says of
|
|
40
|
+
#: which kind), builds what its actions need about the call and returns
|
|
41
|
+
#: ``bound.run(<that>)``, or the text of the error.
|
|
42
|
+
Run = Callable[[Any, Bound], Awaitable[str]]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def tool_annotations(tool: Tool, *, open_world: bool = False) -> ToolAnnotations:
|
|
46
|
+
"""What a client may use to decide whether to ask the person first."""
|
|
47
|
+
from mcp.types import ToolAnnotations
|
|
48
|
+
|
|
49
|
+
if not tool.writes:
|
|
50
|
+
return ToolAnnotations(readOnlyHint=True, openWorldHint=open_world)
|
|
51
|
+
return ToolAnnotations(
|
|
52
|
+
readOnlyHint=False, destructiveHint=tool.destructive, openWorldHint=open_world
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def hints(tool: Tool, *, open_world: bool = False) -> dict[str, Any]:
|
|
57
|
+
"""A tool's annotations as plain data, for a pinned contract."""
|
|
58
|
+
return tool_annotations(tool, open_world=open_world).model_dump(exclude_none=True)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def schema(tool: Tool, *, example_path: str | None = None) -> dict[str, Any]:
|
|
62
|
+
"""The input schema of a tool: ``path`` or ``calls`` (the search: ``q``,
|
|
63
|
+
``t`` or ``calls``), neither required, every other key let through. The
|
|
64
|
+
dispatcher says what is missing."""
|
|
65
|
+
several = (
|
|
66
|
+
f"for several {{what}} at once: a list of up to {MAX_CALLS} calls, each an"
|
|
67
|
+
" object with its own {keys}. Each runs on its own; all are answered together."
|
|
68
|
+
)
|
|
69
|
+
if tool.name == SEARCH:
|
|
70
|
+
first: dict[str, Any] = {
|
|
71
|
+
"q": {
|
|
72
|
+
"type": "string",
|
|
73
|
+
"description": "What to look for, in plain words.",
|
|
74
|
+
},
|
|
75
|
+
KIND_KEY: {
|
|
76
|
+
"type": "string",
|
|
77
|
+
"description": 'Only this kind, e.g. "skill" for the how-to guides.'
|
|
78
|
+
" Left out: every kind.",
|
|
79
|
+
},
|
|
80
|
+
}
|
|
81
|
+
calls = "In place of `q`, " + several.format(
|
|
82
|
+
what="searches", keys="`q` and arguments"
|
|
83
|
+
)
|
|
84
|
+
else:
|
|
85
|
+
example = f', e.g. "{example_path}"' if example_path else ""
|
|
86
|
+
first = {
|
|
87
|
+
PATH_KEY: {
|
|
88
|
+
"type": "string",
|
|
89
|
+
"description": f'Where the call goes: "<package>/<action>"{example}.'
|
|
90
|
+
" With a `?` at the end it returns help.",
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
calls = "In place of `path`, " + several.format(
|
|
94
|
+
what="calls", keys="`path` and arguments"
|
|
95
|
+
)
|
|
96
|
+
return {
|
|
97
|
+
"type": "object",
|
|
98
|
+
"properties": {
|
|
99
|
+
**first,
|
|
100
|
+
CALLS_KEY: {
|
|
101
|
+
"type": "array",
|
|
102
|
+
"items": {"type": "object"},
|
|
103
|
+
"description": calls,
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
"additionalProperties": True,
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _shape(path: str) -> str: # only its metadata is used: text in, text out
|
|
111
|
+
return path
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def listed(registry: Registry) -> list[Tool]:
|
|
115
|
+
"""The tools in the order a client sees them: the host's that only read,
|
|
116
|
+
the search, then the host's that write."""
|
|
117
|
+
host = registry.host_tools
|
|
118
|
+
return [
|
|
119
|
+
*(t for t in host if not t.writes),
|
|
120
|
+
registry.tools[SEARCH],
|
|
121
|
+
*(t for t in host if t.writes),
|
|
122
|
+
]
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def register(
|
|
126
|
+
mcp: FastMCP,
|
|
127
|
+
registry: Registry,
|
|
128
|
+
run: Run,
|
|
129
|
+
*,
|
|
130
|
+
example_path: str | None = None,
|
|
131
|
+
open_world: bool = False,
|
|
132
|
+
) -> None:
|
|
133
|
+
"""Add the registry's tools to ``mcp``. ``run`` makes each checked call
|
|
134
|
+
(see :data:`Run`). ``example_path`` is a path to name in the schema of
|
|
135
|
+
``path``; ``open_world`` says whether the tools reach beyond the host."""
|
|
136
|
+
from mcp.server.fastmcp.tools.base import Tool as McpTool
|
|
137
|
+
from mcp.server.fastmcp.utilities.func_metadata import func_metadata
|
|
138
|
+
|
|
139
|
+
class PathTool(McpTool):
|
|
140
|
+
"""A tool whose arguments are not a fixed signature: the whole object
|
|
141
|
+
goes to the dispatcher."""
|
|
142
|
+
|
|
143
|
+
async def run(
|
|
144
|
+
self,
|
|
145
|
+
arguments: dict[str, Any],
|
|
146
|
+
context: Any = None,
|
|
147
|
+
convert_result: bool = False,
|
|
148
|
+
) -> Any:
|
|
149
|
+
result = await self.fn(arguments, context)
|
|
150
|
+
return self.fn_metadata.convert_result(result) if convert_result else result
|
|
151
|
+
|
|
152
|
+
for tool in listed(registry):
|
|
153
|
+
mcp._tool_manager._tools[tool.name] = PathTool(
|
|
154
|
+
fn=_caller(registry, run, tool.name),
|
|
155
|
+
name=tool.name,
|
|
156
|
+
title=None,
|
|
157
|
+
description=tool.description,
|
|
158
|
+
parameters=schema(tool, example_path=example_path),
|
|
159
|
+
fn_metadata=func_metadata(_shape, structured_output=False),
|
|
160
|
+
is_async=True,
|
|
161
|
+
context_kwarg=None,
|
|
162
|
+
annotations=tool_annotations(tool, open_world=open_world),
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def _caller(
|
|
167
|
+
registry: Registry, run: Run, tool: str
|
|
168
|
+
) -> Callable[[Mapping[str, Any], Any], Awaitable[str]]:
|
|
169
|
+
async def call(arguments: Mapping[str, Any], ctx: Any) -> str:
|
|
170
|
+
try:
|
|
171
|
+
batch = registry.batch(arguments, tool)
|
|
172
|
+
except DispatchError as exc:
|
|
173
|
+
return exc.render()
|
|
174
|
+
if batch is None:
|
|
175
|
+
return await one(arguments, ctx)
|
|
176
|
+
# one after the other, each as if it had come alone
|
|
177
|
+
answers = [(label(item, tool), await one(item, ctx)) for item in batch.calls]
|
|
178
|
+
return repaired(batch.repairs, report(answers))
|
|
179
|
+
|
|
180
|
+
async def one(arguments: Mapping[str, Any], ctx: Any) -> str:
|
|
181
|
+
try:
|
|
182
|
+
found = registry.resolve(tool, arguments)
|
|
183
|
+
except DispatchError as exc:
|
|
184
|
+
return exc.render()
|
|
185
|
+
if isinstance(found, Help):
|
|
186
|
+
return found.text
|
|
187
|
+
try:
|
|
188
|
+
answer = await run(ctx, found)
|
|
189
|
+
except DispatchError as exc:
|
|
190
|
+
# a refusal the host's `run` let through, e.g. so that its
|
|
191
|
+
# transaction rolls back: it reads like every other
|
|
192
|
+
answer = exc.render()
|
|
193
|
+
return repaired(found.repairs, answer)
|
|
194
|
+
|
|
195
|
+
return call
|
mcp_dispatch/py.typed
ADDED
|
File without changes
|