autotool-mcp 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.
- autotool/__init__.py +13 -0
- autotool/__main__.py +3 -0
- autotool/agent.py +106 -0
- autotool/cli.py +214 -0
- autotool/clients/__init__.py +0 -0
- autotool/clients/dynamic_client.py +198 -0
- autotool/core/__init__.py +0 -0
- autotool/core/credentials.py +117 -0
- autotool/core/llm.py +434 -0
- autotool/core/orchestrator.py +201 -0
- autotool/core/policy.py +151 -0
- autotool/core/registry.py +161 -0
- autotool/core/schema.py +142 -0
- autotool/core/toolenv.py +190 -0
- autotool/guard.py +213 -0
- autotool/main.py +111 -0
- autotool/server.py +336 -0
- autotool/setup.py +147 -0
- autotool/synthesis/__init__.py +5 -0
- autotool/synthesis/generator.py +135 -0
- autotool/synthesis/repair.py +84 -0
- autotool/synthesis/verifier.py +252 -0
- autotool_mcp-0.1.0.dist-info/METADATA +285 -0
- autotool_mcp-0.1.0.dist-info/RECORD +28 -0
- autotool_mcp-0.1.0.dist-info/WHEEL +5 -0
- autotool_mcp-0.1.0.dist-info/entry_points.txt +3 -0
- autotool_mcp-0.1.0.dist-info/licenses/LICENSE +21 -0
- autotool_mcp-0.1.0.dist-info/top_level.txt +1 -0
autotool/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""AutoTool: a self-extending tool layer for any AI agent, served over MCP."""
|
|
2
|
+
|
|
3
|
+
__version__ = "0.1.0"
|
|
4
|
+
__all__ = ["anthropic_tools", "connect", "openai_tools", "run_agent"]
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def __getattr__(name: str):
|
|
8
|
+
# Lazy, so tool processes that import autotool.guard don't load the MCP client stack.
|
|
9
|
+
if name in __all__:
|
|
10
|
+
from autotool import agent
|
|
11
|
+
|
|
12
|
+
return getattr(agent, name)
|
|
13
|
+
raise AttributeError(f"module 'autotool' has no attribute {name!r}")
|
autotool/__main__.py
ADDED
autotool/agent.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""Build with AutoTool from Python.
|
|
2
|
+
|
|
3
|
+
import asyncio, autotool
|
|
4
|
+
from autotool.core.llm import default_provider # needs autotool-mcp[models] + OPENAI_API_KEY
|
|
5
|
+
|
|
6
|
+
print(asyncio.run(autotool.run_agent("What's the top story on Hacker News?", default_provider())))
|
|
7
|
+
|
|
8
|
+
Or bring your own agent loop:
|
|
9
|
+
|
|
10
|
+
async with autotool.connect() as session: # AutoTool running as an MCP server
|
|
11
|
+
tools = autotool.openai_tools(await session.list_tools()) # or anthropic_tools(...)
|
|
12
|
+
result = await session.call_tool(name, arguments) # whatever your model asks for
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import os
|
|
18
|
+
import sys
|
|
19
|
+
from collections.abc import AsyncIterator, Callable
|
|
20
|
+
from contextlib import asynccontextmanager
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
from typing import Any
|
|
23
|
+
|
|
24
|
+
from mcp import Client, StdioServerParameters, types
|
|
25
|
+
|
|
26
|
+
from autotool.core.llm import LLMProvider
|
|
27
|
+
from autotool.core.schema import ToolResult
|
|
28
|
+
|
|
29
|
+
SYSTEM = (
|
|
30
|
+
"You are a helpful agent. Use your tools for anything that needs live data or an external API. "
|
|
31
|
+
"If no tool fits, write one with create_tool, then call it. Never invent data a tool should provide."
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@asynccontextmanager
|
|
36
|
+
async def connect(
|
|
37
|
+
home: str | Path | None = None,
|
|
38
|
+
env: dict[str, str] | None = None,
|
|
39
|
+
on_consent: Callable[[str], bool] | None = None,
|
|
40
|
+
server: StdioServerParameters | None = None,
|
|
41
|
+
) -> AsyncIterator[Client]:
|
|
42
|
+
"""Start `autotool serve` as a subprocess and yield a connected MCP client.
|
|
43
|
+
|
|
44
|
+
home: AutoTool's data directory (default ~/.autotool). env: extra environment for the
|
|
45
|
+
server, e.g. {"AUTOTOOL_KEY_TAVILY_API_KEY": "..."}. on_consent: shown AutoTool's approval
|
|
46
|
+
question ("allow tool X to use KEY?"), returns the user's answer; without it, credentialed
|
|
47
|
+
tools need `autotool tools approve`. server: launch parameters to use instead."""
|
|
48
|
+
if server is None:
|
|
49
|
+
server_env = {**os.environ, **(env or {})}
|
|
50
|
+
if home is not None:
|
|
51
|
+
server_env["AUTOTOOL_HOME"] = str(home)
|
|
52
|
+
server = StdioServerParameters(command=sys.executable, args=["-m", "autotool", "serve"], env=server_env)
|
|
53
|
+
|
|
54
|
+
async def elicit(context: Any, params: types.ElicitRequestParams) -> types.ElicitResult:
|
|
55
|
+
return types.ElicitResult(action="accept", content={"allow": bool(on_consent and on_consent(params.message))})
|
|
56
|
+
|
|
57
|
+
async with Client(server, elicitation_callback=elicit if on_consent else None) as client:
|
|
58
|
+
yield client
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def openai_tools(listed: types.ListToolsResult) -> list[dict[str, Any]]:
|
|
62
|
+
"""Tool schemas for the OpenAI Responses API (`tools=`)."""
|
|
63
|
+
return [
|
|
64
|
+
{"type": "function", "name": t.name, "description": t.description or "", "parameters": t.input_schema}
|
|
65
|
+
for t in listed.tools
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def anthropic_tools(listed: types.ListToolsResult) -> list[dict[str, Any]]:
|
|
70
|
+
"""Tool schemas for the Anthropic Messages API (`tools=`)."""
|
|
71
|
+
return [{"name": t.name, "description": t.description or "", "input_schema": t.input_schema} for t in listed.tools]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _text(result: types.CallToolResult) -> str:
|
|
75
|
+
return "\n".join(c.text for c in result.content if isinstance(c, types.TextContent))
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
async def run_agent(
|
|
79
|
+
prompt: str,
|
|
80
|
+
provider: LLMProvider,
|
|
81
|
+
*,
|
|
82
|
+
home: str | Path | None = None,
|
|
83
|
+
env: dict[str, str] | None = None,
|
|
84
|
+
on_consent: Callable[[str], bool] | None = None,
|
|
85
|
+
on_tool: Callable[[str, dict[str, Any], types.CallToolResult], None] | None = None,
|
|
86
|
+
server: StdioServerParameters | None = None,
|
|
87
|
+
max_steps: int = 15,
|
|
88
|
+
) -> str:
|
|
89
|
+
"""A complete agent: the model sees AutoTool's tools, writes new ones when it needs them, and
|
|
90
|
+
answers. Tools are listed every turn, so a tool created in one step is usable in the next."""
|
|
91
|
+
async with connect(home, env, on_consent, server) as session:
|
|
92
|
+
messages: list[Any] = [{"role": "user", "content": prompt}]
|
|
93
|
+
for _ in range(max_steps):
|
|
94
|
+
tools = anthropic_tools(await session.list_tools()) # the providers' neutral shape
|
|
95
|
+
turn = await provider.complete(system=SYSTEM, messages=messages, tools=tools)
|
|
96
|
+
messages.append(provider.assistant_message(turn))
|
|
97
|
+
if not turn.tool_calls:
|
|
98
|
+
return turn.text
|
|
99
|
+
results = []
|
|
100
|
+
for call in turn.tool_calls:
|
|
101
|
+
result = await session.call_tool(call.name, call.arguments)
|
|
102
|
+
if on_tool:
|
|
103
|
+
on_tool(call.name, call.arguments, result)
|
|
104
|
+
results.append(ToolResult(call_id=call.id, content=_text(result), is_error=bool(result.is_error)))
|
|
105
|
+
messages.extend(provider.tool_results_messages(results))
|
|
106
|
+
raise RuntimeError(f"no answer within {max_steps} steps")
|
autotool/cli.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"""`autotool` command line: serve, setup, keys, tools, run."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import asyncio
|
|
7
|
+
import getpass
|
|
8
|
+
import logging
|
|
9
|
+
import os
|
|
10
|
+
import sys
|
|
11
|
+
|
|
12
|
+
import keyring.errors
|
|
13
|
+
|
|
14
|
+
from autotool import __version__
|
|
15
|
+
from autotool.core.credentials import autotool_home, keychain_delete, keychain_names, keychain_set, load_tool_env
|
|
16
|
+
from autotool.core.policy import approve, load_policy, revoke, set_hosts
|
|
17
|
+
from autotool.core.toolenv import RESERVED_PREFIXES, required_env
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def sandbox_on(value: str) -> bool:
|
|
21
|
+
"""Fail safe: only an explicit off/0/false/no turns the guard off."""
|
|
22
|
+
return value.strip().lower() not in {"off", "0", "false", "no"}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def consent_mode(value: str) -> str:
|
|
26
|
+
"""Fail safe: only the explicit "dangerously-allow-all" skips approvals."""
|
|
27
|
+
return "auto" if value.strip().lower() == "dangerously-allow-all" else "prompt"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _keys(args: argparse.Namespace) -> int:
|
|
31
|
+
home = autotool_home()
|
|
32
|
+
if args.action == "add":
|
|
33
|
+
if args.name.upper().startswith(RESERVED_PREFIXES):
|
|
34
|
+
print(
|
|
35
|
+
f"{args.name} is reserved: names starting with {', '.join(RESERVED_PREFIXES)} configure AutoTool itself "
|
|
36
|
+
"and are never given to tools. Store it under another name (e.g. MY_OPENAI_API_KEY).",
|
|
37
|
+
file=sys.stderr,
|
|
38
|
+
)
|
|
39
|
+
return 1
|
|
40
|
+
value = sys.stdin.readline().rstrip("\r\n") if args.stdin else getpass.getpass(f"Value for {args.name} (hidden): ")
|
|
41
|
+
if not value:
|
|
42
|
+
print("No value given; nothing stored.", file=sys.stderr)
|
|
43
|
+
return 1
|
|
44
|
+
try:
|
|
45
|
+
keychain_set(home, args.name, value)
|
|
46
|
+
except ValueError as exc:
|
|
47
|
+
print(exc, file=sys.stderr)
|
|
48
|
+
return 1
|
|
49
|
+
except keyring.errors.KeyringError as exc:
|
|
50
|
+
print(
|
|
51
|
+
f"No OS keychain is available here ({exc}). Put the key in {home / '.env'} instead:\n {args.name}=...",
|
|
52
|
+
file=sys.stderr,
|
|
53
|
+
)
|
|
54
|
+
return 1
|
|
55
|
+
print(f"Stored {args.name} in the OS keychain. Tools that declare it can use it; the model only sees the name.")
|
|
56
|
+
elif args.action == "allow":
|
|
57
|
+
hosts = set_hosts(home, args.name, args.hosts)
|
|
58
|
+
print(f"{args.name}: tools holding it may only reach {', '.join(hosts)}." if hosts else f"{args.name}: no host limit.")
|
|
59
|
+
if args.name not in load_tool_env(home).names:
|
|
60
|
+
print(f"Note: no credential named {args.name} is set yet (names are case-sensitive).", file=sys.stderr)
|
|
61
|
+
elif args.action == "remove":
|
|
62
|
+
keychain_delete(home, args.name)
|
|
63
|
+
print(f"Removed {args.name} from the keychain.")
|
|
64
|
+
else:
|
|
65
|
+
keychain = set(keychain_names(home))
|
|
66
|
+
names = load_tool_env(home).names
|
|
67
|
+
if not names:
|
|
68
|
+
print(f"No credentials yet. Add one with `autotool keys add NAME` or in {home / '.env'}.")
|
|
69
|
+
hosts = load_policy(home)["hosts"]
|
|
70
|
+
for name in names:
|
|
71
|
+
limit = f"hosts: {', '.join(hosts[name])}" if name in hosts else "any host"
|
|
72
|
+
print(f"{name} ({'keychain' if name in keychain else 'env'}; {limit})")
|
|
73
|
+
return 0
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _tools(args: argparse.Namespace) -> int:
|
|
77
|
+
tools_dir = autotool_home() / "tools"
|
|
78
|
+
home = autotool_home()
|
|
79
|
+
pending = home / ".staging" / "pending" / f"{args.name}.py"
|
|
80
|
+
if args.action == "approve":
|
|
81
|
+
# Approve exactly the code the agent submitted (pending) or, failing that, the cached tool.
|
|
82
|
+
source = pending if pending.exists() else tools_dir / f"{args.name}.py"
|
|
83
|
+
if not source.exists():
|
|
84
|
+
print(f"No pending or cached tool named {args.name}; have your agent call create_tool first.", file=sys.stderr)
|
|
85
|
+
return 1
|
|
86
|
+
env = load_tool_env(home)
|
|
87
|
+
approve(home, args.name, args.keys, source.read_text(encoding="utf-8"), env.allowed_hosts(args.keys))
|
|
88
|
+
print(f"Approved {args.name} (this version of its code) to use {', '.join(args.keys)}.")
|
|
89
|
+
return 0
|
|
90
|
+
if args.action == "revoke":
|
|
91
|
+
revoke(home, args.name)
|
|
92
|
+
print(f"Revoked {args.name}'s approval; it will not get credentials until approved again.")
|
|
93
|
+
return 0
|
|
94
|
+
if args.action == "remove":
|
|
95
|
+
path = tools_dir / f"{args.name}.py"
|
|
96
|
+
if not path.exists():
|
|
97
|
+
print(f"No tool named {args.name}.", file=sys.stderr)
|
|
98
|
+
return 1
|
|
99
|
+
path.unlink()
|
|
100
|
+
pending.unlink(missing_ok=True)
|
|
101
|
+
revoke(home, args.name) # a new tool reusing the name must not inherit the approval
|
|
102
|
+
print(f"Removed {args.name} and its approval. Hosts drop it on their next restart.")
|
|
103
|
+
return 0
|
|
104
|
+
scripts = sorted(p for p in tools_dir.glob("*.py") if not p.name.startswith("_")) if tools_dir.exists() else []
|
|
105
|
+
if not scripts:
|
|
106
|
+
print("No tools yet. Your agent creates them with the create_tool tool.")
|
|
107
|
+
for p in scripts:
|
|
108
|
+
try:
|
|
109
|
+
creds = required_env(p.read_text(encoding="utf-8"))
|
|
110
|
+
except (ValueError, SyntaxError):
|
|
111
|
+
creds = ["<unreadable declaration>"]
|
|
112
|
+
approved = load_tool_env(home, consent=True).is_approved(p.stem, creds, p.read_text(encoding="utf-8"))
|
|
113
|
+
state = "" if not creds else (" (approved)" if approved else " (NOT approved)")
|
|
114
|
+
print(f"{p.stem} credentials: {', '.join(creds) or 'none'}{state}")
|
|
115
|
+
return 0
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def main(argv: list[str] | None = None) -> int:
|
|
119
|
+
argv = sys.argv[1:] if argv is None else argv
|
|
120
|
+
if argv[:1] == ["run"]: # the built-in demo agent keeps its own flags
|
|
121
|
+
from autotool.main import cli
|
|
122
|
+
|
|
123
|
+
return cli(argv[1:])
|
|
124
|
+
|
|
125
|
+
parser = argparse.ArgumentParser(prog="autotool", description="A self-extending tool layer for any MCP agent.")
|
|
126
|
+
parser.add_argument("--version", action="version", version=f"autotool-mcp {__version__}")
|
|
127
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
128
|
+
|
|
129
|
+
serve = sub.add_parser("serve", help="Run the MCP server (what your agent host launches).")
|
|
130
|
+
serve.add_argument("--env-file", default=None, help="Extra dotenv file with tool credentials.")
|
|
131
|
+
serve.add_argument("--log-level", default="WARNING")
|
|
132
|
+
serve.add_argument(
|
|
133
|
+
"--consent",
|
|
134
|
+
default=os.environ.get("AUTOTOOL_CONSENT", "prompt"),
|
|
135
|
+
help="'prompt' (default): ask before a tool first gets credentials. 'dangerously-allow-all': "
|
|
136
|
+
"never ask. Same as --dangerously-allow-all-tools.",
|
|
137
|
+
)
|
|
138
|
+
serve.add_argument(
|
|
139
|
+
"--dangerously-allow-all-tools",
|
|
140
|
+
action="store_true",
|
|
141
|
+
help="Skip approvals: every tool gets the credentials it declares without asking you. The guard "
|
|
142
|
+
"and host allowlists still apply.",
|
|
143
|
+
)
|
|
144
|
+
serve.add_argument(
|
|
145
|
+
"--sandbox",
|
|
146
|
+
default=os.environ.get("AUTOTOOL_SANDBOX", "on"),
|
|
147
|
+
help="Run tools under AutoTool's guard (default). Turn off if AutoTool already runs in a sandbox; "
|
|
148
|
+
"host allowlists are then not enforced.",
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
setup = sub.add_parser("setup", help="Connect AutoTool to Claude Code, Codex, Claude Desktop, Cursor and OpenClaw.")
|
|
152
|
+
setup.add_argument(
|
|
153
|
+
"--hosts",
|
|
154
|
+
nargs="*",
|
|
155
|
+
default=None,
|
|
156
|
+
choices=["claude-code", "codex", "claude-desktop", "cursor", "openclaw"],
|
|
157
|
+
help="Limit to these hosts (default: every one detected).",
|
|
158
|
+
)
|
|
159
|
+
setup.add_argument("--dry-run", action="store_true", help="Show what would change without changing anything.")
|
|
160
|
+
setup.add_argument(
|
|
161
|
+
"--launcher",
|
|
162
|
+
choices=["auto", "uvx", "python"],
|
|
163
|
+
default="auto",
|
|
164
|
+
help="How hosts start the server: installed `autotool` script, `uvx autotool-mcp`, or this Python.",
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
keys = sub.add_parser("keys", help="Manage credentials (OS keychain) and their host allowlists.")
|
|
168
|
+
keys_sub = keys.add_subparsers(dest="action", required=True)
|
|
169
|
+
add = keys_sub.add_parser("add", help="Store a credential (hidden prompt).")
|
|
170
|
+
add.add_argument("name")
|
|
171
|
+
add.add_argument("--stdin", action="store_true", help="Read the value from stdin instead of prompting.")
|
|
172
|
+
keys_sub.add_parser("list", help="List credential names (never values) and their host allowlists.")
|
|
173
|
+
allow = keys_sub.add_parser("allow", help="Limit tools holding a key to these hosts (wildcards like *.example.com).")
|
|
174
|
+
allow.add_argument("name")
|
|
175
|
+
allow.add_argument("hosts", nargs="*", help="Hosts; give none to remove the limit.")
|
|
176
|
+
rm = keys_sub.add_parser("remove")
|
|
177
|
+
rm.add_argument("name")
|
|
178
|
+
|
|
179
|
+
tools = sub.add_parser("tools", help="List, approve, revoke or remove created tools.")
|
|
180
|
+
tools_sub = tools.add_subparsers(dest="action", required=True)
|
|
181
|
+
tools_sub.add_parser("list")
|
|
182
|
+
trm = tools_sub.add_parser("remove")
|
|
183
|
+
trm.add_argument("name")
|
|
184
|
+
tap = tools_sub.add_parser("approve", help="Let a tool use these credentials.")
|
|
185
|
+
tap.add_argument("name")
|
|
186
|
+
tap.add_argument("keys", nargs="+")
|
|
187
|
+
trv = tools_sub.add_parser("revoke", help="Withdraw a tool's approval.")
|
|
188
|
+
trv.add_argument("name")
|
|
189
|
+
|
|
190
|
+
sub.add_parser(
|
|
191
|
+
"run",
|
|
192
|
+
help='Demo agent: autotool run "objective". Needs autotool-mcp[models] and a key in ./.env; '
|
|
193
|
+
"it has no consent prompts or allowlists and uses ./tools.",
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
args = parser.parse_args(argv)
|
|
197
|
+
if args.command == "serve":
|
|
198
|
+
logging.basicConfig(level=args.log_level.upper(), stream=sys.stderr, format="%(levelname)s %(name)s: %(message)s")
|
|
199
|
+
from autotool.server import serve as run_server
|
|
200
|
+
|
|
201
|
+
consent = "auto" if args.dangerously_allow_all_tools else consent_mode(args.consent)
|
|
202
|
+
asyncio.run(run_server(args.env_file, consent=consent, sandbox=sandbox_on(args.sandbox)))
|
|
203
|
+
return 0
|
|
204
|
+
if args.command == "setup":
|
|
205
|
+
from autotool.setup import run_setup
|
|
206
|
+
|
|
207
|
+
return run_setup(hosts=args.hosts, dry_run=args.dry_run, launcher=args.launcher)
|
|
208
|
+
if args.command == "keys":
|
|
209
|
+
return _keys(args)
|
|
210
|
+
return _tools(args)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
if __name__ == "__main__":
|
|
214
|
+
raise SystemExit(main())
|
|
File without changes
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"""Async stdio MCP client that spawns a tool script as a child process and keeps
|
|
2
|
+
a live MCP ``Client`` to it, so tools can be called mid-reasoning without
|
|
3
|
+
restarting the parent runtime."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
import re
|
|
9
|
+
import sys
|
|
10
|
+
import tempfile
|
|
11
|
+
from collections.abc import Iterable
|
|
12
|
+
from contextlib import AsyncExitStack
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Any, TextIO
|
|
15
|
+
|
|
16
|
+
from mcp import Client, StdioServerParameters
|
|
17
|
+
from mcp.client.stdio import stdio_client
|
|
18
|
+
from mcp.types import CallToolResult, TextContent
|
|
19
|
+
|
|
20
|
+
from autotool.core.schema import MCPToolDescriptor
|
|
21
|
+
|
|
22
|
+
# Environment variables that must never leak into synthesized code.
|
|
23
|
+
_SECRET_ENV_RE = re.compile(r"(API_KEY|TOKEN|SECRET|PASSWORD|CREDENTIAL)", re.IGNORECASE)
|
|
24
|
+
# What a tool may inherit: OS basics, locale, proxy and CA settings, and `<SERVICE>_API_BASE`
|
|
25
|
+
# overrides. Hosts such as Claude Code hand MCP servers their whole environment (AWS keys,
|
|
26
|
+
# DATABASE_URL, ...), so this is an allowlist, not a denylist.
|
|
27
|
+
_INHERIT = {
|
|
28
|
+
"PATH",
|
|
29
|
+
"PATHEXT",
|
|
30
|
+
"SYSTEMROOT",
|
|
31
|
+
"SYSTEMDRIVE",
|
|
32
|
+
"WINDIR",
|
|
33
|
+
"COMSPEC",
|
|
34
|
+
"TEMP",
|
|
35
|
+
"TMP",
|
|
36
|
+
"TMPDIR",
|
|
37
|
+
"HOME",
|
|
38
|
+
"USERPROFILE",
|
|
39
|
+
"APPDATA",
|
|
40
|
+
"LOCALAPPDATA",
|
|
41
|
+
"LANG",
|
|
42
|
+
"LANGUAGE",
|
|
43
|
+
"TZ",
|
|
44
|
+
"HTTP_PROXY",
|
|
45
|
+
"HTTPS_PROXY",
|
|
46
|
+
"ALL_PROXY",
|
|
47
|
+
"NO_PROXY",
|
|
48
|
+
"SSL_CERT_FILE",
|
|
49
|
+
"SSL_CERT_DIR",
|
|
50
|
+
"REQUESTS_CA_BUNDLE",
|
|
51
|
+
"CURL_CA_BUNDLE",
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _inheritable(name: str) -> bool:
|
|
56
|
+
upper = name.upper()
|
|
57
|
+
return (upper in _INHERIT or upper.startswith("LC_") or upper.endswith("_API_BASE")) and not _SECRET_ENV_RE.search(name)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def sandbox_env(extra: dict[str, str] | None = None, drop: Iterable[str] = ()) -> dict[str, str]:
|
|
61
|
+
"""Allowlisted parent environment minus ``drop`` (every credential-source name, so a tool
|
|
62
|
+
only sees the ones granted to it through ``extra``). Keeps PATH, proxy and CA settings so
|
|
63
|
+
generated tools can still reach the network the host is allowed to reach."""
|
|
64
|
+
dropped = {name.upper() for name in drop}
|
|
65
|
+
env = {k: v for k, v in os.environ.items() if _inheritable(k) and k.upper() not in dropped}
|
|
66
|
+
env["PYTHONUNBUFFERED"] = "1"
|
|
67
|
+
env["PYTHONDONTWRITEBYTECODE"] = "1"
|
|
68
|
+
if extra:
|
|
69
|
+
env.update(extra)
|
|
70
|
+
return env
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def render_call_result(result: CallToolResult) -> str:
|
|
74
|
+
"""Flatten MCP content blocks into text for the LLM."""
|
|
75
|
+
chunks: list[str] = []
|
|
76
|
+
for block in result.content:
|
|
77
|
+
if isinstance(block, TextContent):
|
|
78
|
+
chunks.append(block.text)
|
|
79
|
+
else:
|
|
80
|
+
chunks.append(f"[{block.type} content omitted]")
|
|
81
|
+
if not chunks and result.structured_content is not None:
|
|
82
|
+
chunks.append(str(result.structured_content))
|
|
83
|
+
return "\n".join(chunks)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class DynamicMCPClient:
|
|
87
|
+
"""One live connection to one MCP server script."""
|
|
88
|
+
|
|
89
|
+
def __init__(
|
|
90
|
+
self,
|
|
91
|
+
script_path: str | Path,
|
|
92
|
+
*,
|
|
93
|
+
server_name: str | None = None,
|
|
94
|
+
env: dict[str, str] | None = None,
|
|
95
|
+
call_timeout_s: float = 30.0,
|
|
96
|
+
errlog: TextIO | None = None,
|
|
97
|
+
connect_timeout_s: float | None = None,
|
|
98
|
+
) -> None:
|
|
99
|
+
self.script_path = Path(script_path).resolve()
|
|
100
|
+
self.server_name = server_name or self.script_path.stem
|
|
101
|
+
self.env = env
|
|
102
|
+
self.call_timeout = call_timeout_s
|
|
103
|
+
# Bounds initialize/list_tools; tool calls pass their own timeout.
|
|
104
|
+
self.connect_timeout = connect_timeout_s
|
|
105
|
+
self._stack: AsyncExitStack | None = None
|
|
106
|
+
self._session: Client | None = None
|
|
107
|
+
# Caller-owned errlog outlives the connection (the verifier reads it after
|
|
108
|
+
# a failed startup); otherwise a temp file is managed per connection.
|
|
109
|
+
self._external_errlog = errlog
|
|
110
|
+
self._errlog: TextIO | None = errlog
|
|
111
|
+
self.tools: list[MCPToolDescriptor] = []
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def connected(self) -> bool:
|
|
115
|
+
return self._session is not None
|
|
116
|
+
|
|
117
|
+
async def connect(self) -> list[MCPToolDescriptor]:
|
|
118
|
+
"""Spawn the script with ``sys.executable``, initialize, and list tools.
|
|
119
|
+
|
|
120
|
+
Must be closed from the same asyncio task that opened it (anyio cancel
|
|
121
|
+
scopes are task-bound)."""
|
|
122
|
+
if self._session is not None:
|
|
123
|
+
return self.tools
|
|
124
|
+
env = self.env if self.env is not None else sandbox_env()
|
|
125
|
+
# Every tool runs through autotool.guard: it sends tracebacks to stderr and, when the env
|
|
126
|
+
# carries AUTOTOOL_GUARD, installs the guard. PYTHONPATH lets `-m` find autotool.
|
|
127
|
+
args = ["-m", "autotool.guard", str(self.script_path)]
|
|
128
|
+
env = {**env, "PYTHONPATH": str(Path(__file__).resolve().parents[2])}
|
|
129
|
+
params = StdioServerParameters(command=sys.executable, args=args, env=env, cwd=str(self.script_path.parent))
|
|
130
|
+
stack = AsyncExitStack()
|
|
131
|
+
try:
|
|
132
|
+
# stderr goes to a real file so it can be surfaced on failure.
|
|
133
|
+
if self._external_errlog is None:
|
|
134
|
+
self._errlog = stack.enter_context(
|
|
135
|
+
tempfile.TemporaryFile(mode="w+", encoding="utf-8", prefix=f"{self.server_name}-", suffix=".log")
|
|
136
|
+
)
|
|
137
|
+
transport = stdio_client(params, errlog=self._errlog)
|
|
138
|
+
session = await stack.enter_async_context(Client(transport, read_timeout_seconds=self.connect_timeout))
|
|
139
|
+
self._stack, self._session = stack, session
|
|
140
|
+
await self.refresh_tools()
|
|
141
|
+
except BaseException:
|
|
142
|
+
self._session = None
|
|
143
|
+
await stack.aclose()
|
|
144
|
+
raise
|
|
145
|
+
return self.tools
|
|
146
|
+
|
|
147
|
+
async def refresh_tools(self) -> list[MCPToolDescriptor]:
|
|
148
|
+
listed = await self._require_session().list_tools()
|
|
149
|
+
self.tools = [
|
|
150
|
+
MCPToolDescriptor(
|
|
151
|
+
server=self.server_name,
|
|
152
|
+
name=t.name,
|
|
153
|
+
description=t.description or "",
|
|
154
|
+
input_schema=t.input_schema or {"type": "object", "properties": {}},
|
|
155
|
+
)
|
|
156
|
+
for t in listed.tools
|
|
157
|
+
]
|
|
158
|
+
return self.tools
|
|
159
|
+
|
|
160
|
+
def anthropic_tools(self) -> list[dict[str, Any]]:
|
|
161
|
+
return [t.to_anthropic() for t in self.tools]
|
|
162
|
+
|
|
163
|
+
def openai_tools(self) -> list[dict[str, Any]]:
|
|
164
|
+
return [t.to_openai() for t in self.tools]
|
|
165
|
+
|
|
166
|
+
async def call_tool(self, tool_name: str, arguments: dict[str, Any] | None = None) -> CallToolResult:
|
|
167
|
+
return await self._require_session().call_tool(tool_name, arguments or {}, read_timeout_seconds=self.call_timeout)
|
|
168
|
+
|
|
169
|
+
def read_stderr(self) -> str:
|
|
170
|
+
if self._errlog is None or self._errlog.closed:
|
|
171
|
+
return ""
|
|
172
|
+
self._errlog.flush()
|
|
173
|
+
pos = self._errlog.tell()
|
|
174
|
+
self._errlog.seek(0)
|
|
175
|
+
text = self._errlog.read()
|
|
176
|
+
self._errlog.seek(pos)
|
|
177
|
+
return text
|
|
178
|
+
|
|
179
|
+
async def close(self) -> None:
|
|
180
|
+
"""Close the session; the stdio transport terminates the child process."""
|
|
181
|
+
stack, self._stack, self._session = self._stack, None, None
|
|
182
|
+
if stack is not None:
|
|
183
|
+
try:
|
|
184
|
+
await stack.aclose()
|
|
185
|
+
except BaseException:
|
|
186
|
+
pass
|
|
187
|
+
|
|
188
|
+
def _require_session(self) -> Client:
|
|
189
|
+
if self._session is None:
|
|
190
|
+
raise RuntimeError(f"MCP server '{self.server_name}' is not connected")
|
|
191
|
+
return self._session
|
|
192
|
+
|
|
193
|
+
async def __aenter__(self) -> DynamicMCPClient:
|
|
194
|
+
await self.connect()
|
|
195
|
+
return self
|
|
196
|
+
|
|
197
|
+
async def __aexit__(self, *exc: object) -> None:
|
|
198
|
+
await self.close()
|
|
File without changes
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"""Where tool credentials come from when AutoTool runs as a user-hosted MCP server.
|
|
2
|
+
|
|
3
|
+
Highest precedence first:
|
|
4
|
+
1. the host's MCP config: ``AUTOTOOL_KEY_<NAME>=value`` is offered to tools as ``<NAME>``
|
|
5
|
+
(the prefix makes sharing opt-in; hosts pass their whole environment to the server);
|
|
6
|
+
2. the OS keychain (``autotool keys add NAME``), names indexed in ``~/.autotool/keys.json``;
|
|
7
|
+
3. ``~/.autotool/.env``;
|
|
8
|
+
4. an explicit ``--env-file``.
|
|
9
|
+
|
|
10
|
+
The host project's own ``.env`` is never read implicitly."""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import json
|
|
15
|
+
import logging
|
|
16
|
+
import os
|
|
17
|
+
import re
|
|
18
|
+
from collections.abc import Mapping
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
import keyring
|
|
22
|
+
from dotenv import dotenv_values
|
|
23
|
+
from keyring.errors import KeyringError, PasswordDeleteError
|
|
24
|
+
|
|
25
|
+
from autotool.core.policy import load_policy
|
|
26
|
+
from autotool.core.toolenv import ToolEnv, runtime_dotenv_names, runtime_dotenv_path
|
|
27
|
+
|
|
28
|
+
log = logging.getLogger(__name__)
|
|
29
|
+
|
|
30
|
+
HOME_ENV = "AUTOTOOL_HOME"
|
|
31
|
+
KEY_PREFIX = "AUTOTOOL_KEY_"
|
|
32
|
+
KEYRING_SERVICE = "autotool-mcp"
|
|
33
|
+
_NAME_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def autotool_home() -> Path:
|
|
37
|
+
return Path(os.environ.get(HOME_ENV) or Path.home() / ".autotool")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _index(home: Path) -> Path:
|
|
41
|
+
return home / "keys.json"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def keychain_names(home: Path) -> list[str]:
|
|
45
|
+
try:
|
|
46
|
+
return sorted(json.loads(_index(home).read_text(encoding="utf-8")))
|
|
47
|
+
except FileNotFoundError:
|
|
48
|
+
return []
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _write_index(home: Path, names: set[str]) -> None:
|
|
52
|
+
home.mkdir(parents=True, exist_ok=True)
|
|
53
|
+
_index(home).write_text(json.dumps(sorted(names)), encoding="utf-8")
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def keychain_set(home: Path, name: str, value: str) -> None:
|
|
57
|
+
if not _NAME_RE.match(name):
|
|
58
|
+
raise ValueError(f"{name!r} is not a valid environment variable name (letters, digits, _; not starting with a digit)")
|
|
59
|
+
keyring.set_password(KEYRING_SERVICE, name, value)
|
|
60
|
+
_write_index(home, set(keychain_names(home)) | {name})
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def keychain_delete(home: Path, name: str) -> None:
|
|
64
|
+
try:
|
|
65
|
+
keyring.delete_password(KEYRING_SERVICE, name)
|
|
66
|
+
except PasswordDeleteError:
|
|
67
|
+
pass
|
|
68
|
+
_write_index(home, set(keychain_names(home)) - {name})
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def load_tool_env(
|
|
72
|
+
home: Path | None = None,
|
|
73
|
+
env_file: str | Path | None = None,
|
|
74
|
+
environ: Mapping[str, str] | None = None,
|
|
75
|
+
*,
|
|
76
|
+
consent: bool = False,
|
|
77
|
+
guard: bool = True,
|
|
78
|
+
) -> ToolEnv:
|
|
79
|
+
"""``consent``: tools need the user's approval (policy.json) before receiving keys.
|
|
80
|
+
``guard``: run tools under autotool.guard, which also enforces per-key host allowlists."""
|
|
81
|
+
home = home or autotool_home()
|
|
82
|
+
environ = os.environ if environ is None else environ
|
|
83
|
+
values: dict[str, str | None] = {}
|
|
84
|
+
if env_file is not None:
|
|
85
|
+
if not Path(env_file).is_file():
|
|
86
|
+
raise FileNotFoundError(f"env file not found: {env_file}")
|
|
87
|
+
values.update(dotenv_values(env_file))
|
|
88
|
+
if (home / ".env").is_file():
|
|
89
|
+
values.update(dotenv_values(home / ".env"))
|
|
90
|
+
for name in keychain_names(home):
|
|
91
|
+
try:
|
|
92
|
+
values[name] = keyring.get_password(KEYRING_SERVICE, name)
|
|
93
|
+
except KeyringError as exc: # e.g. headless Linux with no keychain backend
|
|
94
|
+
log.warning("Skipping %s: the OS keychain is unavailable (%s); put it in %s instead", name, exc, home / ".env")
|
|
95
|
+
prefixed = [k for k in environ if k.upper().startswith(KEY_PREFIX)]
|
|
96
|
+
values.update({k[len(KEY_PREFIX) :]: environ[k] for k in prefixed})
|
|
97
|
+
# Strip from tool processes: every source name (via ToolEnv), the prefixed originals, and the
|
|
98
|
+
# .env llm.py loads into the server's own environment.
|
|
99
|
+
policy = load_policy(home)
|
|
100
|
+
from autotool.setup import host_config_paths # host configs may hold AUTOTOOL_KEY_* values
|
|
101
|
+
|
|
102
|
+
protect = [
|
|
103
|
+
home / ".env",
|
|
104
|
+
home / "keys.json",
|
|
105
|
+
home / "policy.json",
|
|
106
|
+
*([env_file] if env_file else []),
|
|
107
|
+
*([runtime_dotenv_path()] if runtime_dotenv_path() else []),
|
|
108
|
+
*host_config_paths(),
|
|
109
|
+
]
|
|
110
|
+
return ToolEnv(
|
|
111
|
+
values,
|
|
112
|
+
also_drop=[*prefixed, *runtime_dotenv_names()],
|
|
113
|
+
hosts=policy["hosts"],
|
|
114
|
+
approvals=policy["approvals"] if consent else None,
|
|
115
|
+
protect=[str(p) for p in protect],
|
|
116
|
+
guard=guard,
|
|
117
|
+
)
|