sharkquant-mcp 0.6.3__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.
- quantdinger_mcp/__init__.py +4 -0
- quantdinger_mcp/__main__.py +5 -0
- quantdinger_mcp/security.py +6 -0
- quantdinger_mcp/server.py +6 -0
- quantdinger_mcp/tool_contract.py +6 -0
- sharkquant_mcp/__init__.py +2 -0
- sharkquant_mcp/security.py +164 -0
- sharkquant_mcp/server.py +1239 -0
- sharkquant_mcp/tool_contract.py +64 -0
- sharkquant_mcp-0.6.3.dist-info/METADATA +152 -0
- sharkquant_mcp-0.6.3.dist-info/RECORD +15 -0
- sharkquant_mcp-0.6.3.dist-info/WHEEL +5 -0
- sharkquant_mcp-0.6.3.dist-info/entry_points.txt +3 -0
- sharkquant_mcp-0.6.3.dist-info/licenses/LICENSE +203 -0
- sharkquant_mcp-0.6.3.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"""Client-side safety rails for the SharkQuant MCP server."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import re
|
|
5
|
+
import time
|
|
6
|
+
from typing import Any, Mapping
|
|
7
|
+
|
|
8
|
+
import httpx
|
|
9
|
+
|
|
10
|
+
# Keep in sync with backend `agent_v1/_security.py`.
|
|
11
|
+
MAX_INDICATOR_CODE_BYTES = 512 * 1024
|
|
12
|
+
|
|
13
|
+
_SECRET_KEYS = frozenset({
|
|
14
|
+
"api_key", "apikey", "secret_key", "secretkey", "passphrase", "secret", "password",
|
|
15
|
+
"private_key", "privatekey", "access_token", "accesstoken", "refresh_token",
|
|
16
|
+
"refreshtoken", "bot_token", "bottoken", "webhook_secret", "webhooksecret",
|
|
17
|
+
"signing_secret", "signingsecret", "client_secret", "clientsecret", "authorization",
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
_SSE_EVENT_RE = re.compile(r"^event:\s*(\S+)\s*$", re.MULTILINE)
|
|
21
|
+
_SSE_DATA_RE = re.compile(r"^data:\s*(.+)$", re.MULTILINE)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def assert_code_size(code: str, *, label: str = "Code") -> None:
|
|
25
|
+
if len((code or "").encode("utf-8")) > MAX_INDICATOR_CODE_BYTES:
|
|
26
|
+
raise ValueError(
|
|
27
|
+
f"{label} exceeds {MAX_INDICATOR_CODE_BYTES // 1024} KiB MCP limit"
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def assert_indicator_code_size(code: str) -> None:
|
|
32
|
+
assert_code_size(code, label="Indicator code")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def assert_json_dict(name: str, value: Any) -> dict:
|
|
36
|
+
if value is None:
|
|
37
|
+
return {}
|
|
38
|
+
if not isinstance(value, dict):
|
|
39
|
+
raise ValueError(f"{name} must be a JSON object")
|
|
40
|
+
return value
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def redact_secrets(value: Any, *, depth: int = 0, max_depth: int = 6) -> Any:
|
|
44
|
+
if depth > max_depth:
|
|
45
|
+
return "***"
|
|
46
|
+
if isinstance(value, Mapping):
|
|
47
|
+
out: dict[str, Any] = {}
|
|
48
|
+
for k, v in value.items():
|
|
49
|
+
key = str(k)
|
|
50
|
+
normalized_key = re.sub(r"[^a-z0-9]+", "_", key.lower()).strip("_")
|
|
51
|
+
if normalized_key in _SECRET_KEYS and v not in (None, "", False):
|
|
52
|
+
out[key] = "***"
|
|
53
|
+
elif isinstance(v, Mapping):
|
|
54
|
+
out[key] = redact_secrets(v, depth=depth + 1, max_depth=max_depth)
|
|
55
|
+
elif isinstance(v, list):
|
|
56
|
+
out[key] = [
|
|
57
|
+
redact_secrets(item, depth=depth + 1, max_depth=max_depth)
|
|
58
|
+
for item in v
|
|
59
|
+
]
|
|
60
|
+
else:
|
|
61
|
+
out[key] = v
|
|
62
|
+
return out
|
|
63
|
+
if isinstance(value, list):
|
|
64
|
+
return [redact_secrets(item, depth=depth + 1, max_depth=max_depth) for item in value]
|
|
65
|
+
return value
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def parse_sse_chunk(text: str) -> list[tuple[str, Any]]:
|
|
69
|
+
"""Parse one or more SSE frames from a text chunk."""
|
|
70
|
+
import json
|
|
71
|
+
|
|
72
|
+
frames: list[tuple[str, Any]] = []
|
|
73
|
+
for block in text.split("\n\n"):
|
|
74
|
+
block = block.strip()
|
|
75
|
+
if not block:
|
|
76
|
+
continue
|
|
77
|
+
event_m = _SSE_EVENT_RE.search(block)
|
|
78
|
+
data_m = _SSE_DATA_RE.search(block)
|
|
79
|
+
if not event_m or not data_m:
|
|
80
|
+
continue
|
|
81
|
+
event = event_m.group(1)
|
|
82
|
+
try:
|
|
83
|
+
payload = json.loads(data_m.group(1))
|
|
84
|
+
except Exception:
|
|
85
|
+
payload = data_m.group(1)
|
|
86
|
+
frames.append((event, payload))
|
|
87
|
+
return frames
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def consume_job_stream(
|
|
91
|
+
client: httpx.Client,
|
|
92
|
+
path: str,
|
|
93
|
+
*,
|
|
94
|
+
since_seq: int = 0,
|
|
95
|
+
max_events: int = 200,
|
|
96
|
+
max_seconds: float = 300.0,
|
|
97
|
+
) -> dict[str, Any]:
|
|
98
|
+
"""Read job SSE until `result` or safety limits are hit."""
|
|
99
|
+
params = {"since": int(since_seq)} if since_seq else None
|
|
100
|
+
events: list[dict[str, Any]] = []
|
|
101
|
+
result: dict[str, Any] | None = None
|
|
102
|
+
truncated = False
|
|
103
|
+
started = time.monotonic()
|
|
104
|
+
|
|
105
|
+
with client.stream("GET", path, params=params) as resp:
|
|
106
|
+
if resp.status_code >= 400:
|
|
107
|
+
try:
|
|
108
|
+
body = resp.json()
|
|
109
|
+
except Exception:
|
|
110
|
+
body = resp.text[:2000]
|
|
111
|
+
return {
|
|
112
|
+
"error": True,
|
|
113
|
+
"status": resp.status_code,
|
|
114
|
+
"body": body,
|
|
115
|
+
"events": events,
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
buffer = ""
|
|
119
|
+
for chunk in resp.iter_text():
|
|
120
|
+
if time.monotonic() - started > max_seconds:
|
|
121
|
+
truncated = True
|
|
122
|
+
break
|
|
123
|
+
buffer += chunk.replace("\r\n", "\n").replace("\r", "\n")
|
|
124
|
+
while "\n\n" in buffer:
|
|
125
|
+
part, buffer = buffer.split("\n\n", 1)
|
|
126
|
+
for event, payload in parse_sse_chunk(part + "\n\n"):
|
|
127
|
+
events.append({"event": event, "data": payload})
|
|
128
|
+
if event == "result":
|
|
129
|
+
result = payload if isinstance(payload, dict) else {"value": payload}
|
|
130
|
+
if len(events) >= max_events and result is None:
|
|
131
|
+
truncated = True
|
|
132
|
+
break
|
|
133
|
+
if truncated or result is not None:
|
|
134
|
+
break
|
|
135
|
+
if truncated or result is not None:
|
|
136
|
+
break
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
"events": events,
|
|
140
|
+
"result": result,
|
|
141
|
+
"truncated": truncated,
|
|
142
|
+
"event_count": len(events),
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def poll_job_until_terminal(
|
|
147
|
+
get_job_fn,
|
|
148
|
+
job_id: str,
|
|
149
|
+
*,
|
|
150
|
+
timeout_s: float = 300.0,
|
|
151
|
+
interval_s: float = 2.0,
|
|
152
|
+
) -> dict[str, Any]:
|
|
153
|
+
"""Poll job snapshot until terminal status or timeout."""
|
|
154
|
+
deadline = time.monotonic() + max(1.0, timeout_s)
|
|
155
|
+
last: Any = None
|
|
156
|
+
while time.monotonic() < deadline:
|
|
157
|
+
last = get_job_fn(job_id)
|
|
158
|
+
if isinstance(last, dict) and last.get("error"):
|
|
159
|
+
return last
|
|
160
|
+
status = (last or {}).get("status") if isinstance(last, dict) else None
|
|
161
|
+
if status in ("succeeded", "failed", "cancelled"):
|
|
162
|
+
return {"job": last, "timed_out": False}
|
|
163
|
+
time.sleep(max(0.5, interval_s))
|
|
164
|
+
return {"job": last, "timed_out": True}
|