codemode-mcp 0.2.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.
- codemode_mcp/__init__.py +19 -0
- codemode_mcp/__main__.py +3 -0
- codemode_mcp/agent.py +127 -0
- codemode_mcp/benchmark.py +230 -0
- codemode_mcp/catalog.py +332 -0
- codemode_mcp/cli.py +395 -0
- codemode_mcp/clients.py +106 -0
- codemode_mcp/config.py +198 -0
- codemode_mcp/core.py +550 -0
- codemode_mcp/dashboard.py +131 -0
- codemode_mcp/oauth.py +216 -0
- codemode_mcp/py.typed +0 -0
- codemode_mcp/sandbox.py +315 -0
- codemode_mcp/sandbox_runner.ts +237 -0
- codemode_mcp/server.py +267 -0
- codemode_mcp/upstream.py +310 -0
- codemode_mcp/usage.py +290 -0
- codemode_mcp-0.2.0.dist-info/METADATA +291 -0
- codemode_mcp-0.2.0.dist-info/RECORD +22 -0
- codemode_mcp-0.2.0.dist-info/WHEEL +4 -0
- codemode_mcp-0.2.0.dist-info/entry_points.txt +3 -0
- codemode_mcp-0.2.0.dist-info/licenses/LICENSE +21 -0
codemode_mcp/config.py
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"""Configuration loading for codemode-mcp.
|
|
2
|
+
|
|
3
|
+
Accepts the de-facto standard ``mcp.json`` format used by Claude Desktop, VS Code,
|
|
4
|
+
Cursor and friends, plus an optional ``codemode`` block for settings::
|
|
5
|
+
|
|
6
|
+
{
|
|
7
|
+
"mcpServers": {
|
|
8
|
+
"github": {"command": "npx", "args": ["-y", "@modelcontextprotocol/server-github"],
|
|
9
|
+
"env": {"GITHUB_TOKEN": "${GITHUB_TOKEN}"}},
|
|
10
|
+
"docs": {"url": "https://example.com/mcp", "headers": {"Authorization": "Bearer ${DOCS_TOKEN}"}}
|
|
11
|
+
},
|
|
12
|
+
"codemode": {"timeout": 30, "max_output_chars": 20000}
|
|
13
|
+
}
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import json
|
|
19
|
+
import os
|
|
20
|
+
import re
|
|
21
|
+
from dataclasses import dataclass, field, fields
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
from typing import Any, Literal
|
|
24
|
+
|
|
25
|
+
_ENV_PATTERN = re.compile(r"\$\{([A-Za-z_][A-Za-z0-9_]*)(?::-([^}]*))?\}")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def expand_env(value: Any) -> Any:
|
|
29
|
+
"""Recursively expand ``${VAR}`` / ``${VAR:-default}`` in strings."""
|
|
30
|
+
if isinstance(value, str):
|
|
31
|
+
|
|
32
|
+
def repl(m: re.Match[str]) -> str:
|
|
33
|
+
name, default = m.group(1), m.group(2)
|
|
34
|
+
env = os.environ.get(name)
|
|
35
|
+
if env is not None:
|
|
36
|
+
return env
|
|
37
|
+
if default is not None:
|
|
38
|
+
return default
|
|
39
|
+
raise ConfigError(f"Environment variable {name!r} referenced in config is not set")
|
|
40
|
+
|
|
41
|
+
return _ENV_PATTERN.sub(repl, value)
|
|
42
|
+
if isinstance(value, list):
|
|
43
|
+
return [expand_env(v) for v in value]
|
|
44
|
+
if isinstance(value, dict):
|
|
45
|
+
return {k: expand_env(v) for k, v in value.items()}
|
|
46
|
+
return value
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class ConfigError(ValueError):
|
|
50
|
+
pass
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass
|
|
54
|
+
class ServerConfig:
|
|
55
|
+
"""One upstream MCP server."""
|
|
56
|
+
|
|
57
|
+
name: str
|
|
58
|
+
command: str | None = None
|
|
59
|
+
args: list[str] = field(default_factory=list)
|
|
60
|
+
env: dict[str, str] | None = None
|
|
61
|
+
cwd: str | None = None
|
|
62
|
+
url: str | None = None
|
|
63
|
+
headers: dict[str, str] | None = None
|
|
64
|
+
transport: Literal["stdio", "http", "sse"] = "stdio"
|
|
65
|
+
include_tools: list[str] | None = None
|
|
66
|
+
exclude_tools: list[str] | None = None
|
|
67
|
+
oauth: dict[str, Any] | None = None
|
|
68
|
+
"""OAuth 2.1 for remote servers: ``true`` or ``{"client_id", "client_secret", "scopes",
|
|
69
|
+
"redirect_port", "client_name"}``. Tokens are cached under ``~/.codemode-mcp/oauth``."""
|
|
70
|
+
|
|
71
|
+
@classmethod
|
|
72
|
+
def from_dict(cls, name: str, raw: dict[str, Any]) -> ServerConfig:
|
|
73
|
+
raw = expand_env(raw)
|
|
74
|
+
transport = raw.get("transport") or raw.get("type")
|
|
75
|
+
if transport in ("streamable-http", "streamable_http", "streamableHttp"):
|
|
76
|
+
transport = "http"
|
|
77
|
+
if transport is None:
|
|
78
|
+
transport = "stdio" if raw.get("command") else "http"
|
|
79
|
+
if transport not in ("stdio", "http", "sse"):
|
|
80
|
+
raise ConfigError(f"Server {name!r}: unsupported transport {transport!r}")
|
|
81
|
+
if transport == "stdio" and not raw.get("command"):
|
|
82
|
+
raise ConfigError(f"Server {name!r}: 'command' is required for stdio servers")
|
|
83
|
+
if transport in ("http", "sse") and not raw.get("url"):
|
|
84
|
+
raise ConfigError(f"Server {name!r}: 'url' is required for {transport} servers")
|
|
85
|
+
oauth = raw.get("oauth")
|
|
86
|
+
if oauth is None and raw.get("auth") == "oauth":
|
|
87
|
+
oauth = {}
|
|
88
|
+
if oauth is True:
|
|
89
|
+
oauth = {}
|
|
90
|
+
if oauth is not None and not isinstance(oauth, dict):
|
|
91
|
+
raise ConfigError(f"Server {name!r}: 'oauth' must be true or an object")
|
|
92
|
+
if oauth is not None and transport == "stdio":
|
|
93
|
+
raise ConfigError(f"Server {name!r}: OAuth is only supported for http/sse servers")
|
|
94
|
+
return cls(
|
|
95
|
+
name=name,
|
|
96
|
+
command=raw.get("command"),
|
|
97
|
+
args=list(raw.get("args") or []),
|
|
98
|
+
env=raw.get("env"),
|
|
99
|
+
cwd=raw.get("cwd"),
|
|
100
|
+
url=raw.get("url"),
|
|
101
|
+
headers=raw.get("headers"),
|
|
102
|
+
transport=transport,
|
|
103
|
+
include_tools=raw.get("include_tools") or raw.get("includeTools"),
|
|
104
|
+
exclude_tools=raw.get("exclude_tools") or raw.get("excludeTools"),
|
|
105
|
+
oauth=oauth,
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
def allows(self, tool_name: str) -> bool:
|
|
109
|
+
if self.include_tools is not None and tool_name not in self.include_tools:
|
|
110
|
+
return False
|
|
111
|
+
return not (self.exclude_tools and tool_name in self.exclude_tools)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
@dataclass
|
|
115
|
+
class Settings:
|
|
116
|
+
"""Tunables for the Code Mode layer."""
|
|
117
|
+
|
|
118
|
+
timeout: float = 30.0
|
|
119
|
+
"""Wall-clock limit (seconds) for one ``run_code`` execution, including tool calls."""
|
|
120
|
+
|
|
121
|
+
tool_call_timeout: float = 60.0
|
|
122
|
+
"""Limit (seconds) for a single upstream tool call."""
|
|
123
|
+
|
|
124
|
+
max_output_chars: int = 20_000
|
|
125
|
+
"""Output of ``run_code`` returned to the LLM is truncated beyond this many characters."""
|
|
126
|
+
|
|
127
|
+
max_tool_calls: int = 200
|
|
128
|
+
"""Upper bound on upstream tool calls per ``run_code`` execution."""
|
|
129
|
+
|
|
130
|
+
inline_catalog_tokens: int = 1_500
|
|
131
|
+
"""If every tool signature fits in this many tokens, they are embedded directly in the
|
|
132
|
+
``run_code`` description and ``search_tools`` is not exposed (one round-trip saved)."""
|
|
133
|
+
|
|
134
|
+
inline_names_tokens: int = 1_500
|
|
135
|
+
"""Otherwise, if the list of tool names fits in this budget it is embedded so the model
|
|
136
|
+
knows what exists; full signatures come from ``search_tools``."""
|
|
137
|
+
|
|
138
|
+
max_search_results: int = 8
|
|
139
|
+
max_description_chars: int = 600
|
|
140
|
+
memory_mb: int = 512
|
|
141
|
+
"""V8 heap limit for the sandbox process."""
|
|
142
|
+
|
|
143
|
+
deno_path: str | None = None
|
|
144
|
+
"""Override the Deno executable (defaults to the one installed by the ``deno`` wheel)."""
|
|
145
|
+
|
|
146
|
+
persistent_state: bool = False
|
|
147
|
+
"""Keep Python variables between ``run_code`` calls (reset on timeout or restart)."""
|
|
148
|
+
|
|
149
|
+
expose_tools: list[str] = field(default_factory=list)
|
|
150
|
+
"""Hybrid mode: ``server.tool`` names (globs allowed) also exposed directly as regular tools."""
|
|
151
|
+
|
|
152
|
+
require_approval: list[str] = field(default_factory=list)
|
|
153
|
+
"""``server.tool`` globs (e.g. ``"*.delete_*"``) that need human approval before each call."""
|
|
154
|
+
|
|
155
|
+
usage_tracking: bool = True
|
|
156
|
+
"""Record token accounting (counts only, never content) for ``codemode-mcp usage`` / dashboard."""
|
|
157
|
+
|
|
158
|
+
usage_db: str | None = None
|
|
159
|
+
"""SQLite path for usage data (default ``~/.codemode-mcp/usage.db``, or ``$CODEMODE_HOME``)."""
|
|
160
|
+
|
|
161
|
+
tokenizer: Literal["auto", "tiktoken", "chars"] = "auto"
|
|
162
|
+
"""Token counting: tiktoken if installed (``auto``), or ~4 chars/token."""
|
|
163
|
+
|
|
164
|
+
audit_log: str | None = None
|
|
165
|
+
"""Optional JSONL file recording every run: code, upstream calls (with args) and output."""
|
|
166
|
+
|
|
167
|
+
@classmethod
|
|
168
|
+
def from_dict(cls, raw: dict[str, Any] | None) -> Settings:
|
|
169
|
+
raw = dict(raw or {})
|
|
170
|
+
known = {f.name for f in fields(cls)}
|
|
171
|
+
unknown = set(raw) - known
|
|
172
|
+
if unknown:
|
|
173
|
+
raise ConfigError(f"Unknown codemode settings: {sorted(unknown)}")
|
|
174
|
+
return cls(**raw)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
@dataclass
|
|
178
|
+
class Config:
|
|
179
|
+
servers: list[ServerConfig]
|
|
180
|
+
settings: Settings = field(default_factory=Settings)
|
|
181
|
+
|
|
182
|
+
@classmethod
|
|
183
|
+
def load(cls, source: str | os.PathLike[str] | dict[str, Any]) -> Config:
|
|
184
|
+
if isinstance(source, dict):
|
|
185
|
+
raw = source
|
|
186
|
+
else:
|
|
187
|
+
path = Path(source)
|
|
188
|
+
try:
|
|
189
|
+
raw = json.loads(path.read_text(encoding="utf-8"))
|
|
190
|
+
except FileNotFoundError:
|
|
191
|
+
raise ConfigError(f"Config file not found: {path}") from None
|
|
192
|
+
except json.JSONDecodeError as e:
|
|
193
|
+
raise ConfigError(f"Invalid JSON in {path}: {e}") from None
|
|
194
|
+
servers_raw = raw.get("mcpServers") or raw.get("servers") or {}
|
|
195
|
+
if not isinstance(servers_raw, dict):
|
|
196
|
+
raise ConfigError("'mcpServers' must be an object mapping names to server configs")
|
|
197
|
+
servers = [ServerConfig.from_dict(name, spec) for name, spec in servers_raw.items() if not spec.get("disabled")]
|
|
198
|
+
return cls(servers=servers, settings=Settings.from_dict(raw.get("codemode")))
|