worldmap-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.
- worldmap_mcp/__init__.py +1 -0
- worldmap_mcp/__main__.py +5 -0
- worldmap_mcp/bridge_client.py +220 -0
- worldmap_mcp/bridge_payload/icon.png +0 -0
- worldmap_mcp/bridge_payload/mod.json +13 -0
- worldmap_mcp/bridge_payload/worldmap_mcp_bridge.gd +8261 -0
- worldmap_mcp/captures.py +66 -0
- worldmap_mcp/cli.py +109 -0
- worldmap_mcp/client_config.py +258 -0
- worldmap_mcp/coast.py +205 -0
- worldmap_mcp/errors.py +121 -0
- worldmap_mcp/forests.py +309 -0
- worldmap_mcp/handshake.py +101 -0
- worldmap_mcp/installer.py +341 -0
- worldmap_mcp/landread.py +439 -0
- worldmap_mcp/layout.py +208 -0
- worldmap_mcp/lifecycle.py +182 -0
- worldmap_mcp/mapcheck.py +362 -0
- worldmap_mcp/maplayers.py +152 -0
- worldmap_mcp/ranges.py +200 -0
- worldmap_mcp/roads.py +240 -0
- worldmap_mcp/server.py +4684 -0
- worldmap_mcp/skills.py +123 -0
- worldmap_mcp/skills_payload/_shared/build-loop.md +100 -0
- worldmap_mcp/skills_payload/_shared/review-rubric.md +91 -0
- worldmap_mcp/skills_payload/_shared/style-bible.md +72 -0
- worldmap_mcp/skills_payload/skill-index.json +61 -0
- worldmap_mcp/skills_payload/worldmap-art-direction/SKILL.md +119 -0
- worldmap_mcp/skills_payload/worldmap-finishing/SKILL.md +130 -0
- worldmap_mcp/skills_payload/worldmap-labels/SKILL.md +122 -0
- worldmap_mcp/skills_payload/worldmap-landmass/SKILL.md +170 -0
- worldmap_mcp/skills_payload/worldmap-settlements/SKILL.md +121 -0
- worldmap_mcp/skills_payload/worldmap-terrain/SKILL.md +207 -0
- worldmap_mcp/skills_payload/worldmap-visual-review/SKILL.md +71 -0
- worldmap_mcp/state_paths.py +53 -0
- worldmap_mcp/terrain.py +353 -0
- worldmap_mcp/timing.py +85 -0
- worldmap_mcp/updates.py +191 -0
- worldmap_mcp-0.2.0.dist-info/METADATA +72 -0
- worldmap_mcp-0.2.0.dist-info/RECORD +44 -0
- worldmap_mcp-0.2.0.dist-info/WHEEL +4 -0
- worldmap_mcp-0.2.0.dist-info/entry_points.txt +2 -0
- worldmap_mcp-0.2.0.dist-info/licenses/LICENSE +21 -0
- worldmap_mcp-0.2.0.dist-info/licenses/NOTICE.md +15 -0
worldmap_mcp/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.2.0"
|
worldmap_mcp/__main__.py
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
import hmac
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import secrets
|
|
8
|
+
import socket
|
|
9
|
+
import time
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from . import timing
|
|
13
|
+
from .errors import (
|
|
14
|
+
BridgeCommandError,
|
|
15
|
+
BridgeError,
|
|
16
|
+
BridgeHandshakeUntrustedError,
|
|
17
|
+
BridgePeerUntrustedError,
|
|
18
|
+
BridgeProtocolError,
|
|
19
|
+
BridgeUnavailableError,
|
|
20
|
+
BridgeUntrustedError,
|
|
21
|
+
ValidationError,
|
|
22
|
+
)
|
|
23
|
+
from .state_paths import STATE_DIR_ENV, token_path
|
|
24
|
+
|
|
25
|
+
MAX_REQUEST_BYTES = 16384 # the bridge's MAX_LINE_BYTES
|
|
26
|
+
|
|
27
|
+
PROTOCOL_VERSION = 2
|
|
28
|
+
DEFAULT_PORT = 8797
|
|
29
|
+
MAX_LINE_BYTES = 1024 * 1024
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
WIRE_ERRORS = "surrogatepass"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _mac(token: str, message: str) -> str:
|
|
36
|
+
return hmac.new(
|
|
37
|
+
token.encode(), message.encode("utf-8", WIRE_ERRORS), hashlib.sha256
|
|
38
|
+
).hexdigest()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _join_surrogates(value: Any) -> Any:
|
|
42
|
+
if isinstance(value, str):
|
|
43
|
+
return value.encode("utf-16", WIRE_ERRORS).decode("utf-16", "replace")
|
|
44
|
+
if isinstance(value, list):
|
|
45
|
+
return [_join_surrogates(item) for item in value]
|
|
46
|
+
if isinstance(value, dict):
|
|
47
|
+
return {_join_surrogates(k): _join_surrogates(v) for k, v in value.items()}
|
|
48
|
+
return value
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def hello_proof(token: str, client_nonce: str, server_nonce: str) -> str:
|
|
52
|
+
return _mac(token, f"wd-mcp/{PROTOCOL_VERSION}|server|{client_nonce}|{server_nonce}")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def message_proof(token: str, role: str, client_nonce: str, server_nonce: str, payload: str) -> str:
|
|
56
|
+
return _mac(
|
|
57
|
+
token, f"wd-mcp/{PROTOCOL_VERSION}|{role}|{client_nonce}|{server_nonce}|1|{payload}"
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _is_nonce(value: object) -> bool:
|
|
62
|
+
return (
|
|
63
|
+
isinstance(value, str) and len(value) == 32 and all(c in "0123456789abcdef" for c in value)
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class BridgeClient:
|
|
68
|
+
def __init__(
|
|
69
|
+
self,
|
|
70
|
+
host: str = "127.0.0.1",
|
|
71
|
+
port: int | None = None,
|
|
72
|
+
timeout: float = 5.0,
|
|
73
|
+
token: str | None = None,
|
|
74
|
+
):
|
|
75
|
+
self.host = host
|
|
76
|
+
self.port = port or int(os.environ.get("WORLDMAP_MCP_PORT", DEFAULT_PORT))
|
|
77
|
+
self.timeout = timeout
|
|
78
|
+
self._token = token
|
|
79
|
+
|
|
80
|
+
def token(self) -> str:
|
|
81
|
+
"""Read on every request, so a regenerated token needs no restart."""
|
|
82
|
+
if self._token is not None:
|
|
83
|
+
return self._token
|
|
84
|
+
path = token_path()
|
|
85
|
+
try:
|
|
86
|
+
value = path.read_text(encoding="utf-8").strip()
|
|
87
|
+
except OSError as exc:
|
|
88
|
+
raise BridgeUnavailableError(
|
|
89
|
+
f"No bridge token at {path}. Run `worldmap-mcp token`, then restart "
|
|
90
|
+
f"Wonderdraft so the bridge reads it. If Wonderdraft or the installer ran from "
|
|
91
|
+
f"a different kind of terminal, set {STATE_DIR_ENV} to the same absolute "
|
|
92
|
+
f"folder for both. ({exc})"
|
|
93
|
+
) from exc
|
|
94
|
+
if not value:
|
|
95
|
+
raise BridgeUnavailableError(f"Bridge token file is empty: {path}")
|
|
96
|
+
return value
|
|
97
|
+
|
|
98
|
+
def request(self, cmd: str, **params: Any) -> dict:
|
|
99
|
+
payload = json.dumps({"cmd": cmd, **params})
|
|
100
|
+
size = len(json.dumps({"seq": 1, "payload": payload, "auth": "0" * 64}).encode()) + 1
|
|
101
|
+
if size > MAX_REQUEST_BYTES:
|
|
102
|
+
raise ValidationError(
|
|
103
|
+
f"request too large: {size} bytes, the bridge takes at most {MAX_REQUEST_BYTES}; "
|
|
104
|
+
"send fewer points or symbols per call"
|
|
105
|
+
)
|
|
106
|
+
token = self.token()
|
|
107
|
+
started = time.perf_counter()
|
|
108
|
+
deadline = time.monotonic() + self.timeout
|
|
109
|
+
try:
|
|
110
|
+
sock = socket.create_connection((self.host, self.port), timeout=self.timeout)
|
|
111
|
+
except OSError as exc:
|
|
112
|
+
raise BridgeUnavailableError(
|
|
113
|
+
f"Nothing answered on {self.host}:{self.port}. Is Wonderdraft running with "
|
|
114
|
+
f"the bridge loaded? ({exc})"
|
|
115
|
+
) from exc
|
|
116
|
+
with sock:
|
|
117
|
+
client_nonce = secrets.token_hex(16)
|
|
118
|
+
self._send(sock, {"cmd": "hello", "protocol": PROTOCOL_VERSION, "nonce": client_nonce})
|
|
119
|
+
answer = self._read(sock, deadline)
|
|
120
|
+
result = answer.get("result")
|
|
121
|
+
if not answer.get("ok") or not isinstance(result, dict):
|
|
122
|
+
raise BridgeProtocolError(
|
|
123
|
+
f"The bridge refused the protocol {PROTOCOL_VERSION} handshake: "
|
|
124
|
+
f"{answer.get('error', answer)}"
|
|
125
|
+
)
|
|
126
|
+
server_nonce = result.get("nonce")
|
|
127
|
+
proof = result.get("proof")
|
|
128
|
+
if (
|
|
129
|
+
result.get("protocol") != PROTOCOL_VERSION
|
|
130
|
+
or not _is_nonce(server_nonce)
|
|
131
|
+
or not isinstance(proof, str)
|
|
132
|
+
or not proof.isascii()
|
|
133
|
+
or not hmac.compare_digest(proof, hello_proof(token, client_nonce, server_nonce))
|
|
134
|
+
):
|
|
135
|
+
raise BridgeHandshakeUntrustedError(
|
|
136
|
+
"The peer could not prove it knows the bridge token; nothing was sent. "
|
|
137
|
+
"Was the token regenerated after Wonderdraft started?"
|
|
138
|
+
)
|
|
139
|
+
self._send(
|
|
140
|
+
sock,
|
|
141
|
+
{
|
|
142
|
+
"seq": 1,
|
|
143
|
+
"payload": payload,
|
|
144
|
+
"auth": message_proof(token, "request", client_nonce, server_nonce, payload),
|
|
145
|
+
},
|
|
146
|
+
)
|
|
147
|
+
envelope = self._read(sock, deadline)
|
|
148
|
+
body = envelope.get("payload")
|
|
149
|
+
auth = envelope.get("auth")
|
|
150
|
+
if isinstance(body, str):
|
|
151
|
+
timing.bridge_request(
|
|
152
|
+
time.perf_counter() - started, len(body.encode("utf-8", WIRE_ERRORS))
|
|
153
|
+
)
|
|
154
|
+
if (
|
|
155
|
+
envelope.get("seq") != 1
|
|
156
|
+
or not isinstance(body, str)
|
|
157
|
+
or not isinstance(auth, str)
|
|
158
|
+
or not auth.isascii()
|
|
159
|
+
or not hmac.compare_digest(
|
|
160
|
+
auth, message_proof(token, "response", client_nonce, server_nonce, body)
|
|
161
|
+
)
|
|
162
|
+
):
|
|
163
|
+
detail = envelope.get("error") if "error" in envelope else "response not authenticated"
|
|
164
|
+
raise BridgePeerUntrustedError(
|
|
165
|
+
f"Bridge response rejected: {detail}. Do not retry: the command may have run."
|
|
166
|
+
)
|
|
167
|
+
try:
|
|
168
|
+
response = json.loads(body)
|
|
169
|
+
except ValueError as exc:
|
|
170
|
+
raise BridgeProtocolError("Authenticated response is not JSON") from exc
|
|
171
|
+
if not isinstance(response, dict):
|
|
172
|
+
raise BridgeProtocolError("Authenticated response must be an object")
|
|
173
|
+
response = _join_surrogates(response)
|
|
174
|
+
if not response.get("ok"):
|
|
175
|
+
raise BridgeCommandError(
|
|
176
|
+
str(response.get("error", "bridge refused the command")), cmd=cmd, params=params
|
|
177
|
+
)
|
|
178
|
+
return response.get("result", {})
|
|
179
|
+
|
|
180
|
+
@staticmethod
|
|
181
|
+
def _send(sock: socket.socket, value: dict) -> None:
|
|
182
|
+
sock.sendall((json.dumps(value) + "\n").encode())
|
|
183
|
+
|
|
184
|
+
@staticmethod
|
|
185
|
+
def _read(sock: socket.socket, deadline: float) -> dict:
|
|
186
|
+
buf = bytearray()
|
|
187
|
+
while b"\n" not in buf:
|
|
188
|
+
remaining = deadline - time.monotonic()
|
|
189
|
+
if remaining <= 0:
|
|
190
|
+
raise BridgeProtocolError("Bridge did not answer in time")
|
|
191
|
+
sock.settimeout(remaining)
|
|
192
|
+
try:
|
|
193
|
+
chunk = sock.recv(65536)
|
|
194
|
+
except TimeoutError as exc:
|
|
195
|
+
raise BridgeProtocolError("Bridge did not answer in time") from exc
|
|
196
|
+
if not chunk:
|
|
197
|
+
raise BridgeProtocolError("Bridge closed the connection without a full line")
|
|
198
|
+
buf.extend(chunk)
|
|
199
|
+
if len(buf) > MAX_LINE_BYTES:
|
|
200
|
+
raise BridgeProtocolError("Bridge line exceeded the byte limit")
|
|
201
|
+
line = bytes(buf[: buf.index(b"\n")])
|
|
202
|
+
try:
|
|
203
|
+
value = json.loads(line.decode("utf-8", WIRE_ERRORS))
|
|
204
|
+
except (ValueError, UnicodeDecodeError) as exc:
|
|
205
|
+
raise BridgeProtocolError("Unparseable line from bridge") from exc
|
|
206
|
+
if not isinstance(value, dict):
|
|
207
|
+
raise BridgeProtocolError("Bridge line must be a JSON object")
|
|
208
|
+
return value
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
__all__ = [
|
|
212
|
+
"BridgeClient",
|
|
213
|
+
"BridgeCommandError",
|
|
214
|
+
"BridgeError",
|
|
215
|
+
"BridgeHandshakeUntrustedError",
|
|
216
|
+
"BridgePeerUntrustedError",
|
|
217
|
+
"BridgeProtocolError",
|
|
218
|
+
"BridgeUnavailableError",
|
|
219
|
+
"BridgeUntrustedError",
|
|
220
|
+
]
|
|
Binary file
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"app": "wonderdraft",
|
|
3
|
+
"id": "worldmap-mcp",
|
|
4
|
+
"name": "Worldmap MCP bridge",
|
|
5
|
+
"version": "0.2.0",
|
|
6
|
+
"entry": "worldmap_mcp_bridge.gd",
|
|
7
|
+
"icon": "icon.png",
|
|
8
|
+
"supported_wonderdraft": [
|
|
9
|
+
"1.1.8.2"
|
|
10
|
+
],
|
|
11
|
+
"repo": "thekannen/worldmap-mcp",
|
|
12
|
+
"docs": "https://github.com/thekannen/worldmap-mcp#readme"
|
|
13
|
+
}
|