imperal-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.
- imperal_mcp/__init__.py +1 -0
- imperal_mcp/client.py +88 -0
- imperal_mcp/config.py +19 -0
- imperal_mcp/gate.py +20 -0
- imperal_mcp/irkit.py +134 -0
- imperal_mcp/mask.py +62 -0
- imperal_mcp/server.py +144 -0
- imperal_mcp-0.1.0.dist-info/METADATA +20 -0
- imperal_mcp-0.1.0.dist-info/RECORD +12 -0
- imperal_mcp-0.1.0.dist-info/WHEEL +4 -0
- imperal_mcp-0.1.0.dist-info/entry_points.txt +2 -0
- imperal_mcp-0.1.0.dist-info/licenses/LICENSE +196 -0
imperal_mcp/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
imperal_mcp/client.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
|
|
7
|
+
from .config import Config
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ImperalAuthError(RuntimeError):
|
|
11
|
+
pass
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ImperalError(RuntimeError):
|
|
15
|
+
def __init__(self, message: str, status_code: int | None = None):
|
|
16
|
+
super().__init__(message)
|
|
17
|
+
self.status_code = status_code
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class ImperalClient:
|
|
21
|
+
def __init__(self, cfg: Config):
|
|
22
|
+
self._cfg = cfg
|
|
23
|
+
self._imperal_id: str | None = None
|
|
24
|
+
|
|
25
|
+
def _headers(self) -> dict[str, str]:
|
|
26
|
+
if not self._cfg.token:
|
|
27
|
+
raise ImperalAuthError("IMPERAL_TOKEN is not set — cannot call the Imperal API.")
|
|
28
|
+
return {"Authorization": f"Bearer {self._cfg.token}"}
|
|
29
|
+
|
|
30
|
+
async def _request(self, method: str, path: str, *, json: Any = None) -> Any:
|
|
31
|
+
url = f"{self._cfg.api_url}{path}"
|
|
32
|
+
async with httpx.AsyncClient(timeout=60) as cli:
|
|
33
|
+
resp = await cli.request(method, url, json=json, headers=self._headers())
|
|
34
|
+
if resp.status_code >= 400:
|
|
35
|
+
raise ImperalError(f"{method} {path} -> {resp.status_code}: {resp.text[:300]}", status_code=resp.status_code)
|
|
36
|
+
return resp.json()
|
|
37
|
+
|
|
38
|
+
async def whoami(self) -> str:
|
|
39
|
+
if self._imperal_id is None:
|
|
40
|
+
data = await self._request("GET", "/v1/auth/me")
|
|
41
|
+
self._imperal_id = data.get("imperal_id") or data.get("id")
|
|
42
|
+
if not self._imperal_id:
|
|
43
|
+
raise ImperalError("GET /v1/auth/me did not return imperal_id")
|
|
44
|
+
return self._imperal_id
|
|
45
|
+
|
|
46
|
+
async def ensure_app(self, app_id: str, display_name: str) -> None:
|
|
47
|
+
try:
|
|
48
|
+
await self._request("POST", "/v1/developer/apps", json={
|
|
49
|
+
"app_id": app_id,
|
|
50
|
+
"display_name": display_name or app_id,
|
|
51
|
+
"git_url": f"https://imperal.io/ir-apps/{app_id}",
|
|
52
|
+
})
|
|
53
|
+
except ImperalError as e:
|
|
54
|
+
msg = str(e).lower()
|
|
55
|
+
if e.status_code == 409 or "already in use" in msg or "exists" in msg:
|
|
56
|
+
return # already created — fine
|
|
57
|
+
raise
|
|
58
|
+
|
|
59
|
+
async def _dev_call(self, function: str, params: dict) -> dict:
|
|
60
|
+
uid = await self.whoami()
|
|
61
|
+
return await self._request("POST", "/v1/extensions/developer/call", json={
|
|
62
|
+
"user_id": uid, "tenant_id": "default", "function": function, "params": params,
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
async def deploy_ir(self, app_id: str, ir_dict: dict) -> dict:
|
|
66
|
+
return await self._dev_call("deploy_ir", {"app_id": app_id, "ir_dict": ir_dict})
|
|
67
|
+
|
|
68
|
+
async def smoke_ir(self, ir_dict: dict, function: str, args: dict) -> dict:
|
|
69
|
+
return await self._dev_call("smoke_ir", {"ir_dict": ir_dict, "function": function, "args": args})
|
|
70
|
+
|
|
71
|
+
async def list_apps(self) -> list[dict]:
|
|
72
|
+
return await self._request("GET", "/v1/developer/apps")
|
|
73
|
+
|
|
74
|
+
async def get_app(self, app_id: str) -> dict:
|
|
75
|
+
return await self._request("GET", f"/v1/developer/apps/{app_id}")
|
|
76
|
+
|
|
77
|
+
async def get_marketplace_app(self, app_id: str) -> dict:
|
|
78
|
+
"""Fetch app manifest from the marketplace catalog. Returns {} on 404/error."""
|
|
79
|
+
try:
|
|
80
|
+
return await self._request("GET", f"/v1/marketplace/apps/{app_id}")
|
|
81
|
+
except ImperalError:
|
|
82
|
+
return {}
|
|
83
|
+
|
|
84
|
+
async def run_tool(self, app_id: str, function: str, params: dict) -> dict:
|
|
85
|
+
uid = await self.whoami()
|
|
86
|
+
return await self._request("POST", f"/v1/extensions/{app_id}/call", json={
|
|
87
|
+
"user_id": uid, "tenant_id": "default", "function": function, "params": params,
|
|
88
|
+
})
|
imperal_mcp/config.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
|
|
6
|
+
DEFAULT_API_URL = "https://auth.imperal.io"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass(frozen=True)
|
|
10
|
+
class Config:
|
|
11
|
+
api_url: str
|
|
12
|
+
token: str | None
|
|
13
|
+
|
|
14
|
+
@classmethod
|
|
15
|
+
def from_env(cls) -> "Config":
|
|
16
|
+
return cls(
|
|
17
|
+
api_url=os.environ.get("IMPERAL_API_URL", DEFAULT_API_URL).rstrip("/"),
|
|
18
|
+
token=os.environ.get("IMPERAL_TOKEN") or None,
|
|
19
|
+
)
|
imperal_mcp/gate.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
|
|
5
|
+
_SYNTHETIC_PREFIXES = ("__panel__", "__widget__", "__webhook__", "skeleton_", "_internal_")
|
|
6
|
+
_LEGACY_CHAT = re.compile(r"^tool_.*_chat$")
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def is_synthetic(name: str) -> bool:
|
|
10
|
+
return any((name or "").startswith(p) for p in _SYNTHETIC_PREFIXES)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def is_read_only(name: str, action_type: str | None) -> bool:
|
|
14
|
+
"""Fail-closed: runnable iff action_type == 'read', not synthetic, and not a
|
|
15
|
+
legacy tool_*_chat BYOLLM orchestrator (opaque effective action)."""
|
|
16
|
+
if is_synthetic(name):
|
|
17
|
+
return False
|
|
18
|
+
if _LEGACY_CHAT.match(name or ""):
|
|
19
|
+
return False
|
|
20
|
+
return action_type == "read"
|
imperal_mcp/irkit.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
|
|
5
|
+
from imperal_sdk.ir.validator import validate_ir_dict
|
|
6
|
+
from imperal_sdk.ir.actions import validate_step
|
|
7
|
+
import imperal_sdk.ui as _ui
|
|
8
|
+
|
|
9
|
+
# Canonical link-saver example — schema-valid (passes validate_ir).
|
|
10
|
+
# Uses real declarative step shape: op + args{kind, data}.
|
|
11
|
+
LINK_SAVER_EXAMPLE: dict = {
|
|
12
|
+
"ir_version": "1.0",
|
|
13
|
+
"app": {
|
|
14
|
+
"id": "link-saver",
|
|
15
|
+
"version": "1.0.0",
|
|
16
|
+
"title": "Link Saver",
|
|
17
|
+
"capabilities": ["store"],
|
|
18
|
+
"functions": [
|
|
19
|
+
{
|
|
20
|
+
"name": "save_link",
|
|
21
|
+
"action_type": "write",
|
|
22
|
+
"params_schema": {"url": {"type": "string"}},
|
|
23
|
+
"return_schema": {"type": "object"},
|
|
24
|
+
"impl": {
|
|
25
|
+
"kind": "declarative",
|
|
26
|
+
"steps": [
|
|
27
|
+
{
|
|
28
|
+
"id": "s1",
|
|
29
|
+
"op": "store.create",
|
|
30
|
+
"args": {"kind": "link", "data": {"url": "{{args.url}}"}},
|
|
31
|
+
}
|
|
32
|
+
],
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
"name": "list_links",
|
|
37
|
+
"action_type": "read",
|
|
38
|
+
"params_schema": {},
|
|
39
|
+
"return_schema": {"type": "array"},
|
|
40
|
+
"impl": {
|
|
41
|
+
"kind": "declarative",
|
|
42
|
+
"steps": [
|
|
43
|
+
{
|
|
44
|
+
"id": "s1",
|
|
45
|
+
"op": "store.list",
|
|
46
|
+
"args": {"kind": "link"},
|
|
47
|
+
}
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _issue(rule: str, level: str, message: str) -> dict:
|
|
57
|
+
return {"rule": rule, "level": level, "message": message}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def validate_ir(app_ir: dict) -> dict:
|
|
61
|
+
"""Full local validation: envelope structure (validate_ir_dict) + every
|
|
62
|
+
declarative step (validate_step). No network. valid == no ERROR issues."""
|
|
63
|
+
issues: list[dict] = [
|
|
64
|
+
_issue(i.rule, i.level, i.message) for i in validate_ir_dict(app_ir)
|
|
65
|
+
]
|
|
66
|
+
# Per-step validation for declarative function impls.
|
|
67
|
+
for fn in (app_ir.get("app", {}) or {}).get("functions", []) or []:
|
|
68
|
+
impl = fn.get("impl") or {}
|
|
69
|
+
for idx, step in enumerate(impl.get("steps", []) or []):
|
|
70
|
+
for msg in validate_step(step):
|
|
71
|
+
issues.append(
|
|
72
|
+
_issue("STEP", "ERROR", f"{fn.get('name', '?')}[{idx}]: {msg}")
|
|
73
|
+
)
|
|
74
|
+
valid = not any(i["level"] == "ERROR" for i in issues)
|
|
75
|
+
return {"valid": valid, "issues": issues}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def ui_catalog_text() -> str:
|
|
79
|
+
names = sorted(getattr(_ui, "__all__", []))
|
|
80
|
+
lines = ["# Imperal ui.* component catalog", ""]
|
|
81
|
+
lines += [f"- ui.{n}" for n in names]
|
|
82
|
+
lines.append("")
|
|
83
|
+
lines.append("Each component is a UINode {type, props}; compose them in panels/render.")
|
|
84
|
+
return "\n".join(lines)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def ir_spec_text() -> str:
|
|
88
|
+
return (
|
|
89
|
+
"# Imperal IR envelope (app.ir.json)\n\n"
|
|
90
|
+
"An IR app is a declarative envelope:\n"
|
|
91
|
+
"{\n"
|
|
92
|
+
' "ir_version": "1.0",\n'
|
|
93
|
+
' "app": {\n'
|
|
94
|
+
' "id": "<app_id>", "version": "1.0.0", "title": "<display name>",\n'
|
|
95
|
+
' "capabilities": ["store"],\n'
|
|
96
|
+
' "functions": [\n'
|
|
97
|
+
' {"name": "save_link", "action_type": "write",\n'
|
|
98
|
+
' "params_schema": {"url": {"type": "string"}},\n'
|
|
99
|
+
' "return_schema": {"type": "object"},\n'
|
|
100
|
+
' "impl": {"kind": "declarative", "steps": [\n'
|
|
101
|
+
' {"id": "s1", "op": "store.create",\n'
|
|
102
|
+
' "args": {"kind": "link", "data": {"url": "{{args.url}}"}}}\n'
|
|
103
|
+
" ]}}\n"
|
|
104
|
+
" ]\n"
|
|
105
|
+
" }\n}\n\n"
|
|
106
|
+
"Declarative step vocabulary:\n"
|
|
107
|
+
"- op: store.create / store.list / store.get / store.update / store.delete\n"
|
|
108
|
+
"- args: {kind, data} for create/update; {kind} for list/get/delete\n"
|
|
109
|
+
"- Use {{args.<param>}} template syntax to reference function parameters.\n\n"
|
|
110
|
+
"- Every function declares action_type: read | write | destructive.\n"
|
|
111
|
+
"- Validate locally with the validate_ir tool BEFORE deploy.\n"
|
|
112
|
+
"- Render with ui.* components (see the ui-catalog resource)."
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def examples_text() -> str:
|
|
117
|
+
return (
|
|
118
|
+
"# Example app.ir.json — link-saver\n\n"
|
|
119
|
+
"```json\n"
|
|
120
|
+
+ json.dumps(LINK_SAVER_EXAMPLE, indent=2)
|
|
121
|
+
+ "\n```\n"
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def build_prompt_text() -> str:
|
|
126
|
+
return (
|
|
127
|
+
"You are building an Imperal app as a declarative app.ir.json. Steps:\n"
|
|
128
|
+
"1. Read the imperal://ir-spec and imperal://ui-catalog resources.\n"
|
|
129
|
+
"2. Author the app.ir.json envelope. Declare each function's action_type.\n"
|
|
130
|
+
"3. Call validate_ir(app_ir) and fix every ERROR issue.\n"
|
|
131
|
+
"4. Call smoke_ir(app_ir, function, args) to run it in isolation and confirm it works.\n"
|
|
132
|
+
"5. Call deploy_ir(app_ir, app_id) to deploy. The app is then live and routable.\n"
|
|
133
|
+
"Keep functions small; never invent verbs outside the action vocabulary."
|
|
134
|
+
)
|
imperal_mcp/mask.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Verbatim port of the gateway app/util/pii.py (SOURCE OF TRUTH: imperal_kernel/audit/_pii.py).
|
|
2
|
+
# Keep regexes + ALLOWLIST_KEYS IN SYNC with the gateway/kernel — drift hazard.
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from enum import Enum
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class PIIRedactionLevel(str, Enum):
|
|
11
|
+
NONE = "none"
|
|
12
|
+
MASK_PII = "mask_pii"
|
|
13
|
+
FULL_REDACT = "full_redact"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
_EMAIL_RE = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}")
|
|
17
|
+
_PHONE_RE = re.compile(r"\+?\d[\d\s\-().]{7,}\d")
|
|
18
|
+
_SSN_RE = re.compile(r"\b\d{3}-\d{2}-\d{4}\b")
|
|
19
|
+
_CC_RE = re.compile(r"\b(?:\d[ -]?){13,19}\b")
|
|
20
|
+
|
|
21
|
+
ALLOWLIST_KEYS = frozenset({
|
|
22
|
+
"id", "app_id", "imperal_id", "user_id", "tenant_id", "owner_id",
|
|
23
|
+
"developer_id", "agency_id", "commit_sha", "sha", "version",
|
|
24
|
+
"created_at", "updated_at", "timestamp", "ts",
|
|
25
|
+
"request_id", "trace_id", "message_id", "task_id",
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def apply_pii_redaction(text: str | None, level: PIIRedactionLevel) -> str | None:
|
|
30
|
+
if text is None:
|
|
31
|
+
return None
|
|
32
|
+
if not text:
|
|
33
|
+
return text
|
|
34
|
+
if level == PIIRedactionLevel.NONE:
|
|
35
|
+
return text
|
|
36
|
+
if level == PIIRedactionLevel.FULL_REDACT:
|
|
37
|
+
return "<redacted>"
|
|
38
|
+
masked = _SSN_RE.sub("<SSN>", text)
|
|
39
|
+
masked = _CC_RE.sub("<CARD>", masked)
|
|
40
|
+
masked = _EMAIL_RE.sub("<EMAIL>", masked)
|
|
41
|
+
masked = _PHONE_RE.sub("<PHONE>", masked)
|
|
42
|
+
return masked
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def mask_pii_in_obj(obj: Any, level: PIIRedactionLevel = PIIRedactionLevel.MASK_PII) -> Any:
|
|
46
|
+
if isinstance(obj, dict):
|
|
47
|
+
out = {}
|
|
48
|
+
for k, v in obj.items():
|
|
49
|
+
if isinstance(v, str) and k in ALLOWLIST_KEYS:
|
|
50
|
+
out[k] = v
|
|
51
|
+
else:
|
|
52
|
+
out[k] = mask_pii_in_obj(v, level)
|
|
53
|
+
return out
|
|
54
|
+
if isinstance(obj, list):
|
|
55
|
+
return [mask_pii_in_obj(v, level) for v in obj]
|
|
56
|
+
if isinstance(obj, str):
|
|
57
|
+
return apply_pii_redaction(obj, level)
|
|
58
|
+
return obj
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
# Back-compat alias — server.py applies the masker via this name on every read egress.
|
|
62
|
+
defensive_scrub = mask_pii_in_obj
|
imperal_mcp/server.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import json
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from mcp.server.fastmcp import FastMCP
|
|
8
|
+
|
|
9
|
+
from .client import ImperalClient
|
|
10
|
+
from .config import Config
|
|
11
|
+
from .gate import is_read_only, is_synthetic
|
|
12
|
+
from .irkit import (
|
|
13
|
+
validate_ir as _validate_ir,
|
|
14
|
+
ui_catalog_text,
|
|
15
|
+
ir_spec_text,
|
|
16
|
+
examples_text,
|
|
17
|
+
build_prompt_text,
|
|
18
|
+
)
|
|
19
|
+
from .mask import defensive_scrub
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _tools_of(app: dict) -> list[dict]:
|
|
23
|
+
"""Normalize a get_app payload to a list of {name, action_type}.
|
|
24
|
+
|
|
25
|
+
Handles all real shapes returned by the gateway:
|
|
26
|
+
1. tools_json is a list → return directly.
|
|
27
|
+
2. manifest_json is a dict → return manifest_json["tools"].
|
|
28
|
+
3. manifest_json is a non-empty JSON string → parse it, return ["tools"].
|
|
29
|
+
4. Anything else → [].
|
|
30
|
+
"""
|
|
31
|
+
tj = app.get("tools_json")
|
|
32
|
+
if isinstance(tj, list):
|
|
33
|
+
return tj
|
|
34
|
+
mj = app.get("manifest_json")
|
|
35
|
+
if isinstance(mj, dict):
|
|
36
|
+
return mj.get("tools", []) or []
|
|
37
|
+
if isinstance(mj, str) and mj:
|
|
38
|
+
try:
|
|
39
|
+
parsed = json.loads(mj)
|
|
40
|
+
return parsed.get("tools", []) or []
|
|
41
|
+
except (json.JSONDecodeError, AttributeError):
|
|
42
|
+
return []
|
|
43
|
+
return []
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
async def _resolve_action_type(client: ImperalClient, app_id: str, function: str) -> str | None:
|
|
47
|
+
app = await client.get_app(app_id)
|
|
48
|
+
for t in _tools_of(app):
|
|
49
|
+
if t.get("name") == function:
|
|
50
|
+
return t.get("action_type")
|
|
51
|
+
# Fallback: check the marketplace catalog
|
|
52
|
+
try:
|
|
53
|
+
mkt = await client.get_marketplace_app(app_id)
|
|
54
|
+
for t in (mkt.get("tools") or []):
|
|
55
|
+
if t.get("name") == function:
|
|
56
|
+
return t.get("action_type")
|
|
57
|
+
except Exception:
|
|
58
|
+
pass
|
|
59
|
+
return None
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
async def deploy_ir_logic(client: ImperalClient, app_ir: dict, app_id: str) -> dict:
|
|
63
|
+
"""Deploy app_ir; retry once after 1 s on a transient first-deploy failure."""
|
|
64
|
+
result = await client.deploy_ir(app_id, app_ir)
|
|
65
|
+
if result.get("status") != "success":
|
|
66
|
+
await asyncio.sleep(1.0)
|
|
67
|
+
result = await client.deploy_ir(app_id, app_ir)
|
|
68
|
+
return result
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
async def run_read_tool_logic(client: ImperalClient, app_id: str, function: str, args: dict) -> Any:
|
|
72
|
+
if is_synthetic(function):
|
|
73
|
+
return {"refused": True, "reason": "synthetic system entry, not a callable tool"}
|
|
74
|
+
action_type = await _resolve_action_type(client, app_id, function)
|
|
75
|
+
if not is_read_only(function, action_type):
|
|
76
|
+
return {"refused": True,
|
|
77
|
+
"reason": f"tool '{function}' is action_type={action_type!r}; "
|
|
78
|
+
"this MCP runs read-only tools only"}
|
|
79
|
+
out = await client.run_tool(app_id, function, args or {})
|
|
80
|
+
return defensive_scrub(out)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def build_server(client: ImperalClient) -> FastMCP:
|
|
84
|
+
mcp = FastMCP("imperal")
|
|
85
|
+
|
|
86
|
+
@mcp.tool()
|
|
87
|
+
def validate_ir(app_ir: dict) -> dict:
|
|
88
|
+
"""Validate an app.ir.json locally (envelope + every declarative step)."""
|
|
89
|
+
return _validate_ir(app_ir)
|
|
90
|
+
|
|
91
|
+
@mcp.tool()
|
|
92
|
+
async def smoke_ir(app_ir: dict, function: str, args: dict | None = None) -> dict:
|
|
93
|
+
"""Run one function of an app.ir.json in an ISOLATED store and report {ok,result,trace}."""
|
|
94
|
+
return await client.smoke_ir(app_ir, function, args or {})
|
|
95
|
+
|
|
96
|
+
@mcp.tool()
|
|
97
|
+
async def deploy_ir(app_ir: dict, app_id: str) -> dict:
|
|
98
|
+
"""Deploy an app.ir.json into the caller's account (creates the app record if needed)."""
|
|
99
|
+
display = (app_ir.get("app", {}) or {}).get("id", app_id)
|
|
100
|
+
await client.ensure_app(app_id, display)
|
|
101
|
+
return await deploy_ir_logic(client, app_ir, app_id)
|
|
102
|
+
|
|
103
|
+
@mcp.tool()
|
|
104
|
+
async def list_apps() -> Any:
|
|
105
|
+
"""List the caller's developer apps (PII-masked)."""
|
|
106
|
+
return defensive_scrub(await client.list_apps())
|
|
107
|
+
|
|
108
|
+
@mcp.tool()
|
|
109
|
+
async def get_app(app_id: str) -> Any:
|
|
110
|
+
"""Get one app's manifest + tools (with action_type) (PII-masked)."""
|
|
111
|
+
return defensive_scrub(await client.get_app(app_id))
|
|
112
|
+
|
|
113
|
+
@mcp.tool()
|
|
114
|
+
async def run_read_tool(app_id: str, function: str, args: dict | None = None) -> Any:
|
|
115
|
+
"""Run a READ-only tool of a deployed app (refuses write/destructive)."""
|
|
116
|
+
return await run_read_tool_logic(client, app_id, function, args or {})
|
|
117
|
+
|
|
118
|
+
@mcp.resource("imperal://ir-spec")
|
|
119
|
+
def _r_spec() -> str:
|
|
120
|
+
return ir_spec_text()
|
|
121
|
+
|
|
122
|
+
@mcp.resource("imperal://ui-catalog")
|
|
123
|
+
def _r_ui() -> str:
|
|
124
|
+
return ui_catalog_text()
|
|
125
|
+
|
|
126
|
+
@mcp.resource("imperal://examples")
|
|
127
|
+
def _r_ex() -> str:
|
|
128
|
+
return examples_text()
|
|
129
|
+
|
|
130
|
+
@mcp.prompt()
|
|
131
|
+
def build_imperal_app() -> str:
|
|
132
|
+
"""Guidance for building an Imperal app from intent."""
|
|
133
|
+
return build_prompt_text()
|
|
134
|
+
|
|
135
|
+
return mcp
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def main() -> None:
|
|
139
|
+
client = ImperalClient(Config.from_env())
|
|
140
|
+
build_server(client).run() # stdio transport
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
if __name__ == "__main__":
|
|
144
|
+
main()
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: imperal-mcp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: MCP server for building Imperal apps from any LLM/agent
|
|
5
|
+
Project-URL: Homepage, https://imperal.io
|
|
6
|
+
Project-URL: Repository, https://github.com/imperalcloud/imperal-mcp
|
|
7
|
+
Author: Imperal, Inc.
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Requires-Python: >=3.11
|
|
14
|
+
Requires-Dist: httpx>=0.27
|
|
15
|
+
Requires-Dist: imperal-sdk>=5.7.3
|
|
16
|
+
Requires-Dist: mcp>=1.2.0
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
19
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
20
|
+
Requires-Dist: respx>=0.21; extra == 'dev'
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
imperal_mcp/__init__.py,sha256=kUR5RAFc7HCeiqdlX36dZOHkUI5wI6V_43RpEcD8b-0,22
|
|
2
|
+
imperal_mcp/client.py,sha256=Dh6Xp_LpTd-Gx1zzNs7-hSKXd08OiOWjbRtHFEWkDYg,3509
|
|
3
|
+
imperal_mcp/config.py,sha256=nMfIypg9cubYweaVXzirV8-Mph6eQ-Cztbmm52J6-j4,430
|
|
4
|
+
imperal_mcp/gate.py,sha256=b7vV8J2hKzmW0I4IFAp5VLKg8txw8quhKaHF0VLkr68,654
|
|
5
|
+
imperal_mcp/irkit.py,sha256=QlXu6MleiCn7XNZ-DvpjkDYQNMtHY_O85PumBzof3fA,5085
|
|
6
|
+
imperal_mcp/mask.py,sha256=bY9Wox3JGp1cLKNHH_Nv1GPA5elXypAgf0SGmdw3F9w,2018
|
|
7
|
+
imperal_mcp/server.py,sha256=Kp9Dlqc7U6SjFI3i84UbelArNqV_-M0s-7qRkfKcfPU,4867
|
|
8
|
+
imperal_mcp-0.1.0.dist-info/METADATA,sha256=P5JYBVzu9CylgTfk2v5Rp36kAUFj85UITQ52U-sjsBc,727
|
|
9
|
+
imperal_mcp-0.1.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
|
|
10
|
+
imperal_mcp-0.1.0.dist-info/entry_points.txt,sha256=izywuUsEjGhTpDjDaS2wH32y_xwBll8u0hNbzYutQ2I,56
|
|
11
|
+
imperal_mcp-0.1.0.dist-info/licenses/LICENSE,sha256=yrcmbOToAcpYu4CDgNaBHifiyFRt6TToiHknTerdgMg,11080
|
|
12
|
+
imperal_mcp-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work (an example is provided in the Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submitted" means any form of electronic, verbal,
|
|
51
|
+
or written communication sent to the Licensor or its representatives,
|
|
52
|
+
including but not limited to communication on electronic mailing lists,
|
|
53
|
+
source code control systems, and issue tracking systems that are managed
|
|
54
|
+
by, or on behalf of, the Licensor for the purpose of tracking and
|
|
55
|
+
modifying the Work, but excluding communication that is conspicuously
|
|
56
|
+
marked or designated in writing by the copyright owner as "Not a
|
|
57
|
+
Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
60
|
+
whom a Contribution has been received by the Licensor and included
|
|
61
|
+
within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
64
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
65
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
66
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
67
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
68
|
+
Work and such Derivative Works in Source or Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
(except as stated in this section) patent license to make, have made,
|
|
74
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
75
|
+
where such license applies only to those patent contributions
|
|
76
|
+
Licensable by such Contributor that are necessarily infringed by
|
|
77
|
+
their Contribution(s) alone or by the combination of their
|
|
78
|
+
Contribution(s) with the Work to which such Contribution(s) was
|
|
79
|
+
submitted. If You institute patent litigation against any entity
|
|
80
|
+
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
|
81
|
+
the Work or any contribution embodied within the Work constitutes a
|
|
82
|
+
direct or contributory patent infringement, then any patent licenses
|
|
83
|
+
granted to You under this License for that Work shall terminate as of
|
|
84
|
+
the date such litigation is filed.
|
|
85
|
+
|
|
86
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
87
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
88
|
+
modifications, and in Source or Object form, provided that You
|
|
89
|
+
meet the following conditions:
|
|
90
|
+
|
|
91
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
92
|
+
Works a copy of this License; and
|
|
93
|
+
|
|
94
|
+
(b) You must cause any modified files to carry prominent notices
|
|
95
|
+
stating that You changed the files; and
|
|
96
|
+
|
|
97
|
+
(c) You must retain, in the Source form of any Work that You
|
|
98
|
+
distribute, all copyright, patent, trademark, and attribution
|
|
99
|
+
notices from the Source form of the Work, excluding those notices
|
|
100
|
+
that do not pertain to any part of the Derivative Works; and
|
|
101
|
+
|
|
102
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
103
|
+
distribution, You must include a readable copy of the attribution
|
|
104
|
+
notices contained within such NOTICE file, in at least one of the
|
|
105
|
+
following places: within a NOTICE text file distributed as part of
|
|
106
|
+
the Derivative Works; within the Source form or documentation, if
|
|
107
|
+
provided along with the Derivative Works; or, within a display
|
|
108
|
+
generated by the Derivative Works, if and wherever such third-party
|
|
109
|
+
notices normally appear. The contents of the NOTICE file are for
|
|
110
|
+
informational purposes only and do not modify the License. You may
|
|
111
|
+
add Your own attribution notices within Derivative Works that You
|
|
112
|
+
distribute, alongside or in addition to the NOTICE text from the
|
|
113
|
+
Work, provided that such additional attribution notices cannot be
|
|
114
|
+
construed as modifying the License.
|
|
115
|
+
|
|
116
|
+
You may add Your own license statement for Your modifications and
|
|
117
|
+
may provide additional grant of rights to use, copy, modify, merge,
|
|
118
|
+
publish, distribute, sublicense, and/or sell copies of the
|
|
119
|
+
Derivative Works, and to permit persons to whom the Derivative Works
|
|
120
|
+
is furnished to do so, subject to the following conditions described
|
|
121
|
+
in this License.
|
|
122
|
+
|
|
123
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
124
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
125
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
126
|
+
this License, without any additional terms or conditions.
|
|
127
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
128
|
+
the terms of any separate license agreement you may have executed
|
|
129
|
+
with Licensor regarding such Contributions.
|
|
130
|
+
|
|
131
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
132
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
133
|
+
except as required for reasonable and customary use in describing the
|
|
134
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
135
|
+
|
|
136
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
137
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
138
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
139
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
140
|
+
implied, including, without limitation, any warranties or conditions
|
|
141
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
142
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
143
|
+
appropriateness of using or reproducing the Work and assume any
|
|
144
|
+
risks associated with Your exercise of permissions under this License.
|
|
145
|
+
|
|
146
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
147
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
148
|
+
unless required by applicable law (such as deliberate and grossly
|
|
149
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
150
|
+
liable to You for damages, including any direct, indirect, special,
|
|
151
|
+
incidental, or exemplary damages of any character arising as a
|
|
152
|
+
result of this License or out of the use or inability to use the
|
|
153
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
154
|
+
work stoppage, computer failure or malfunction, or all other
|
|
155
|
+
commercial damages or losses), even if such Contributor has been
|
|
156
|
+
advised of the possibility of such damages.
|
|
157
|
+
|
|
158
|
+
9. Accepting Warranty or Liability. While redistributing the Work or
|
|
159
|
+
Derivative Works thereof, You may choose to offer, and charge a fee
|
|
160
|
+
for, acceptance of support, warranty, indemnity, or other liability
|
|
161
|
+
obligations and/or rights consistent with this License. However, in
|
|
162
|
+
accepting such obligations, You may offer such conditions only on
|
|
163
|
+
Your own behalf and on a full legally responsible basis, not on
|
|
164
|
+
behalf of any other Contributor, and only if You agree to indemnify,
|
|
165
|
+
defend, and hold each Contributor harmless for any liability incurred
|
|
166
|
+
by, or claims asserted against, such Contributor by reason of your
|
|
167
|
+
accepting any warranty or additional liability.
|
|
168
|
+
|
|
169
|
+
END OF TERMS AND CONDITIONS
|
|
170
|
+
|
|
171
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
172
|
+
|
|
173
|
+
To apply the Apache License to your work, attach the following
|
|
174
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
175
|
+
replaced with your own identifying information. (Don't include
|
|
176
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
177
|
+
comment syntax for the format in question. Please include the
|
|
178
|
+
boilerplate notice with the fields enclosed by brackets "[]"
|
|
179
|
+
replaced with your own identifying information. (Don't include
|
|
180
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
181
|
+
comment syntax for the format in question. Please also include
|
|
182
|
+
any NOTICE file as described in the Appendix.
|
|
183
|
+
|
|
184
|
+
Copyright 2026 Imperal, Inc.
|
|
185
|
+
|
|
186
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
187
|
+
you may not use this file except in compliance with the License.
|
|
188
|
+
You may obtain a copy of the License at
|
|
189
|
+
|
|
190
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
191
|
+
|
|
192
|
+
Unless required by applicable law or agreed to in writing, software
|
|
193
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
194
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
195
|
+
See the License for the specific language governing permissions and
|
|
196
|
+
limitations under the License.
|