arael-sketch 0.5.2

Interactive 2D sketch editor with real-time constraint solving
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// MCP (Model Context Protocol) server embedded in the sketch editor.
// Runs an async HTTP server (axum/tokio) in a background thread.
// Communicates with the GUI thread via channels.
//
// Protocol: Streamable HTTP (MCP spec 2025-03-26)
// Spec: https://modelcontextprotocol.io/specification/

#![cfg(not(target_arch = "wasm32"))]

use std::collections::{HashMap, HashSet};
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use tokio::sync::{mpsc, oneshot};
use axum::{
    extract::{Query, State},
    http::{HeaderMap, StatusCode, Method, Uri},
    middleware::{self, Next},
    response::{IntoResponse, Response, Redirect},
    routing::{get, post, delete},
    Json, Router,
};
use eframe::egui;
use serde_json::{json, Value};
use sha2::{Sha256, Digest};
use base64::engine::{Engine, general_purpose::URL_SAFE_NO_PAD};
use rand::Rng;

/// A request from the MCP server to the GUI thread.
pub struct McpRequest {
    pub command: String,
    pub response_tx: oneshot::Sender<String>,
    pub blocked_commands: Vec<&'static str>,
}

#[derive(Clone)]
struct McpState {
    tx: Arc<mpsc::Sender<McpRequest>>,
    verbose: bool,
    egui_ctx: Arc<Mutex<Option<egui::Context>>>,
    session_id: Arc<Mutex<Option<String>>>,
    addr: SocketAddr,
    allow_all: bool,
    // OAuth state: auth_code -> (code_challenge, client_id)
    auth_codes: Arc<Mutex<HashMap<String, (String, String)>>>,
    valid_tokens: Arc<Mutex<HashSet<String>>>,
}

/// Start the MCP server on the given address.
/// Returns a receiver for commands that the GUI thread should poll.
pub fn start(
    addr: SocketAddr,
    verbose: bool,
    allow_all: bool,
    egui_ctx: Arc<Mutex<Option<egui::Context>>>,
) -> mpsc::Receiver<McpRequest> {
    let (tx, rx) = mpsc::channel::<McpRequest>(32);
    let state = McpState {
        tx: Arc::new(tx),
        verbose,
        egui_ctx,
        session_id: Arc::new(Mutex::new(None)),
        addr,
        allow_all,
        auth_codes: Arc::new(Mutex::new(HashMap::new())),
        valid_tokens: Arc::new(Mutex::new(HashSet::new())),
    };
    std::thread::spawn(move || {
        let rt = tokio::runtime::Runtime::new()
            .expect("Failed to create tokio runtime for MCP server");
        rt.block_on(async move {
            let app = Router::new()
                .route("/mcp", post(handle_post))
                .route("/mcp", get(handle_get))
                .route("/mcp", delete(handle_delete))
                // OAuth 2.1 endpoints for Claude Code compatibility
                .route("/.well-known/oauth-authorization-server", get(handle_oauth_metadata))
                .route("/.well-known/oauth-authorization-server/mcp", get(handle_oauth_metadata))
                .route("/register", post(handle_register))
                .route("/authorize", get(handle_authorize))
                .route("/token", post(handle_token))
                .fallback(handle_fallback)
                .layer(middleware::from_fn_with_state(state.clone(), log_middleware))
                .with_state(state);
            let listener = tokio::net::TcpListener::bind(addr).await
                .unwrap_or_else(|e| panic!("MCP server failed to bind {}: {}", addr, e));
            eprintln!("MCP server listening on http://{}/mcp", addr);
            axum::serve(listener, app).await.unwrap();
        });
    });
    rx
}

// ---------------------------------------------------------------------------
// Middleware: log all requests when verbose
// ---------------------------------------------------------------------------

async fn log_middleware(
    State(state): State<McpState>,
    method: Method,
    uri: Uri,
    request: axum::extract::Request,
    next: Next,
) -> Response {
    if state.verbose {
        eprintln!("[MCP] {} {}", method, uri);
    }
    let response = next.run(request).await;
    if state.verbose {
        eprintln!("[MCP] {} {} -> {}", method, uri, response.status());
    }
    response
}

// ---------------------------------------------------------------------------
// Route handlers
// ---------------------------------------------------------------------------

/// Wake the GUI thread so it polls the MCP channel.
fn wake_gui(state: &McpState) {
    if let Some(ctx) = state.egui_ctx.lock().unwrap().as_ref() {
        ctx.request_repaint();
    }
}

/// Build response headers (session ID, content-type).
fn response_headers(state: &McpState) -> HeaderMap {
    let mut headers = HeaderMap::new();
    if let Some(ref sid) = *state.session_id.lock().unwrap() {
        headers.insert("Mcp-Session-Id", sid.parse().unwrap());
    }
    headers
}

async fn handle_post(
    State(state): State<McpState>,
    headers: HeaderMap,
    body: String,
) -> Response {
    if state.verbose {
        eprintln!("MCP <<< {}", body);
    }

    let request: Value = match serde_json::from_str(&body) {
        Ok(v) => v,
        Err(e) => {
            return (StatusCode::BAD_REQUEST, response_headers(&state), Json(json!({
                "jsonrpc": "2.0", "id": null,
                "error": { "code": -32700, "message": format!("Parse error: {}", e) }
            }))).into_response();
        }
    };

    let method = request.get("method").and_then(|v| v.as_str()).unwrap_or("");
    let id = request.get("id").cloned().unwrap_or(Value::Null);

    // Validate session ID on non-initialize requests
    if method != "initialize" {
        let current_sid = state.session_id.lock().unwrap().clone();
        if let Some(ref expected) = current_sid {
            let client_sid = headers.get("mcp-session-id").and_then(|v| v.to_str().ok());
            if client_sid != Some(expected.as_str()) {
                return (StatusCode::NOT_FOUND, response_headers(&state), Json(json!({
                    "jsonrpc": "2.0", "id": id,
                    "error": { "code": -32600, "message": "Invalid or missing session ID" }
                }))).into_response();
            }
        }
    }

    let mut resp_headers = response_headers(&state);
    let (resp_body, no_body) = match method {
        "initialize" => {
            let sid = generate_session_id();
            *state.session_id.lock().unwrap() = Some(sid.clone());
            resp_headers.insert("Mcp-Session-Id", sid.parse().unwrap());
            (handle_initialize(id, &request, &state).await, false)
        }
        "notifications/initialized" => (Value::Null, true),
        "tools/list" => (handle_tools_list(id), false),
        "tools/call" => (handle_tools_call(id, &request, &state).await, false),
        "resources/list" => (handle_resources_list(id), false),
        "resources/read" => (handle_resources_read(id, &request), false),
        _ => (json!({
            "jsonrpc": "2.0", "id": id,
            "error": { "code": -32601, "message": format!("Method not found: {}", method) }
        }), false),
    };

    if state.verbose {
        if no_body {
            eprintln!("MCP >>> (no body)");
        } else {
            eprintln!("MCP >>> {}", resp_body);
        }
    }

    if no_body {
        (StatusCode::ACCEPTED, resp_headers).into_response()
    } else {
        (StatusCode::OK, resp_headers, Json(resp_body)).into_response()
    }
}

/// GET /mcp - SSE streaming (not supported, return 405)
async fn handle_get() -> Response {
    StatusCode::METHOD_NOT_ALLOWED.into_response()
}

/// DELETE /mcp - session termination (not supported, return 405)
async fn handle_delete() -> Response {
    StatusCode::METHOD_NOT_ALLOWED.into_response()
}

// ---------------------------------------------------------------------------
// OAuth 2.1 endpoints (minimal implementation for Claude Code compatibility)
// ---------------------------------------------------------------------------

fn random_hex(len: usize) -> String {
    let mut rng = rand::rng();
    (0..len).map(|_| format!("{:02x}", rng.random::<u8>())).collect()
}

/// OAuth authorization server metadata (RFC 8414)
async fn handle_oauth_metadata(state: State<McpState>) -> Json<Value> {
    let base = format!("http://{}", state.addr);
    Json(json!({
        "issuer": base,
        "authorization_endpoint": format!("{}/authorize", base),
        "token_endpoint": format!("{}/token", base),
        "registration_endpoint": format!("{}/register", base),
        "response_types_supported": ["code"],
        "grant_types_supported": ["authorization_code"],
        "code_challenge_methods_supported": ["S256"]
    }))
}

/// Dynamic client registration (RFC 7591) — accept any client
async fn handle_register(Json(body): Json<Value>) -> Json<Value> {
    let client_name = body.get("client_name").and_then(|v| v.as_str()).unwrap_or("unknown");
    let client_id = random_hex(16);
    Json(json!({
        "client_id": client_id,
        "client_name": client_name,
        "redirect_uris": body.get("redirect_uris").cloned().unwrap_or(json!([])),
        "grant_types": ["authorization_code"],
        "response_types": ["code"],
        "token_endpoint_auth_method": "none"
    }))
}

/// Authorization endpoint — auto-approve and redirect back with auth code
async fn handle_authorize(state: State<McpState>, Query(params): Query<HashMap<String, String>>) -> Response {
    let redirect_uri = match params.get("redirect_uri") {
        Some(uri) => uri.clone(),
        None => return (StatusCode::BAD_REQUEST, Json(json!({"error": "missing redirect_uri"}))).into_response(),
    };
    let code_challenge = params.get("code_challenge").cloned().unwrap_or_default();
    let client_id = params.get("client_id").cloned().unwrap_or_default();
    let request_state = params.get("state").cloned().unwrap_or_default();

    if !state.allow_all {
        // Future: GUI approval prompt. For now, reject.
        return (StatusCode::FORBIDDEN, Json(json!({"error": "access_denied", "error_description": "GUI approval not yet implemented. Use --mcp-allow-all"}))).into_response();
    }

    // Generate auth code and store with challenge for PKCE verification
    let code = random_hex(32);
    state.auth_codes.lock().unwrap().insert(code.clone(), (code_challenge, client_id));

    // Redirect back to client with code
    let sep = if redirect_uri.contains('?') { '&' } else { '?' };
    let redirect_url = format!("{}{}code={}&state={}", redirect_uri, sep, code, request_state);
    Redirect::to(&redirect_url).into_response()
}

/// Token endpoint — exchange auth code + PKCE verifier for bearer token
async fn handle_token(state: State<McpState>, body: String) -> Response {
    let params: HashMap<String, String> = form_urlencoded::parse(body.as_bytes())
        .map(|(k, v)| (k.into_owned(), v.into_owned()))
        .collect();

    let code = params.get("code").cloned().unwrap_or_default();
    let code_verifier = params.get("code_verifier").cloned().unwrap_or_default();

    // Look up the auth code
    let stored = state.auth_codes.lock().unwrap().remove(&code);
    let (code_challenge, _client_id) = match stored {
        Some(v) => v,
        None => return (StatusCode::BAD_REQUEST, Json(json!({"error": "invalid_grant"}))).into_response(),
    };

    // PKCE S256 verification: SHA256(code_verifier) base64url-encoded == code_challenge
    if !code_challenge.is_empty() {
        let mut hasher = Sha256::new();
        hasher.update(code_verifier.as_bytes());
        let hash = hasher.finalize();
        let computed = base64url_encode(&hash);
        if computed != code_challenge {
            return (StatusCode::BAD_REQUEST, Json(json!({"error": "invalid_grant", "error_description": "PKCE verification failed"}))).into_response();
        }
    }

    // Issue bearer token
    let access_token = random_hex(32);
    state.valid_tokens.lock().unwrap().insert(access_token.clone());

    Json(json!({
        "access_token": access_token,
        "token_type": "Bearer",
        "expires_in": 86400
    })).into_response()
}

fn base64url_encode(data: &[u8]) -> String {
    URL_SAFE_NO_PAD.encode(data)
}

/// Fallback for unknown paths - return JSON 404 (not empty body)
async fn handle_fallback(method: Method, uri: Uri) -> Response {
    (StatusCode::NOT_FOUND, Json(json!({
        "error": format!("Not found: {} {}", method, uri)
    }))).into_response()
}

// ---------------------------------------------------------------------------
// MCP method handlers
// ---------------------------------------------------------------------------

async fn handle_initialize(id: Value, request: &Value, state: &McpState) -> Value {
    let client_name = request.pointer("/params/clientInfo/name").and_then(|v| v.as_str()).unwrap_or("unknown");
    let client_version = request.pointer("/params/clientInfo/version").and_then(|v| v.as_str()).unwrap_or("?");
    eprintln!("MCP: agent connected: {} v{}", client_name, client_version);
    // Notify GUI via a msg command
    let msg = format!("msg **MCP agent connected:** {} v{}", client_name, client_version);
    let _ = send_command_str(&state.tx, &msg, state).await;
    json!({
        "jsonrpc": "2.0",
        "id": id,
        "result": {
            "protocolVersion": "2025-03-26",
            "capabilities": {
                "tools": { "listChanged": false },
                "resources": { "subscribe": false, "listChanged": false }
            },
            "serverInfo": {
                "name": "arael-sketch",
                "version": env!("CARGO_PKG_VERSION")
            },
            "instructions": format!("{}\n\n{}", MCP_PREAMBLE, include_str!("../docs/COMMANDS.md"))
        }
    })
}

fn handle_tools_list(id: Value) -> Value {
    json!({
        "jsonrpc": "2.0",
        "id": id,
        "result": {
            "tools": [
                {
                    "name": "execute_command",
                    "description": "Execute a single sketch command and return the result. See instructions for available commands.",
                    "inputSchema": {
                        "type": "object",
                        "properties": {
                            "command": {
                                "type": "string",
                                "description": "The sketch command to execute, e.g. 'add_line 0,0 5,0' or 'horizontal L0'"
                            }
                        },
                        "required": ["command"]
                    }
                },
                {
                    "name": "execute_script",
                    "description": "Execute multiple sketch commands (one per line) and return all results.",
                    "inputSchema": {
                        "type": "object",
                        "properties": {
                            "script": {
                                "type": "string",
                                "description": "Multiple commands separated by newlines"
                            }
                        },
                        "required": ["script"]
                    }
                },
                {
                    "name": "get_sketch_state",
                    "description": "Get the current sketch state: all entities, dimensions, parameters, and constraints.",
                    "inputSchema": {
                        "type": "object",
                        "properties": {}
                    }
                },
                {
                    "name": "get_help",
                    "description": "Get the full command reference documentation.",
                    "inputSchema": {
                        "type": "object",
                        "properties": {}
                    }
                }
            ]
        }
    })
}

async fn handle_tools_call(id: Value, request: &Value, state: &McpState) -> Value {
    let name = request.pointer("/params/name").and_then(|v| v.as_str()).unwrap_or("");
    let args = request.pointer("/params/arguments").cloned().unwrap_or(json!({}));

    match name {
        "execute_command" => {
            let command = args.get("command").and_then(|v| v.as_str()).unwrap_or("");
            if command.is_empty() {
                return tool_error(id, "Missing 'command' argument");
            }
            let result = send_command_str(&state.tx, command, state).await;
            tool_result(id, &result)
        }
        "execute_script" => {
            let script = args.get("script").and_then(|v| v.as_str()).unwrap_or("");
            if script.is_empty() {
                return tool_error(id, "Missing 'script' argument");
            }
            let mut outputs = Vec::new();
            for line in script.lines() {
                let line = line.trim();
                if line.is_empty() || line.starts_with('#') { continue; }
                let result = send_command_str(&state.tx, line, state).await;
                outputs.push(result);
            }
            tool_result(id, &outputs.join("\n"))
        }
        "get_sketch_state" => {
            let result = send_command_str(&state.tx, "list", state).await;
            tool_result(id, &result)
        }
        "get_help" => {
            tool_result(id, include_str!("../docs/COMMANDS.md"))
        }
        _ => {
            json!({
                "jsonrpc": "2.0", "id": id,
                "error": { "code": -32602, "message": format!("Unknown tool: {}", name) }
            })
        }
    }
}

fn handle_resources_list(id: Value) -> Value {
    json!({
        "jsonrpc": "2.0",
        "id": id,
        "result": {
            "resources": [
                {
                    "uri": "sketch://commands",
                    "name": "Command Reference",
                    "description": "Full sketch command documentation",
                    "mimeType": "text/markdown"
                },
                {
                    "uri": "sketch://state",
                    "name": "Sketch State",
                    "description": "Current sketch entities, constraints, and parameters",
                    "mimeType": "text/plain"
                }
            ]
        }
    })
}

fn handle_resources_read(id: Value, request: &Value) -> Value {
    let uri = request.pointer("/params/uri").and_then(|v| v.as_str()).unwrap_or("");
    match uri {
        "sketch://commands" => json!({
            "jsonrpc": "2.0", "id": id,
            "result": {
                "contents": [{
                    "uri": "sketch://commands",
                    "mimeType": "text/markdown",
                    "text": include_str!("../docs/COMMANDS.md")
                }]
            }
        }),
        _ => json!({
            "jsonrpc": "2.0", "id": id,
            "error": { "code": -32602, "message": format!("Unknown resource: {}", uri) }
        }),
    }
}

// ---------------------------------------------------------------------------
// Channel communication with GUI
// ---------------------------------------------------------------------------

const MCP_BLOCKED: &[&str] = &["save", "load", "exit", "quit"];

async fn send_command_str(tx: &mpsc::Sender<McpRequest>, command: &str, state: &McpState) -> String {
    let (resp_tx, resp_rx) = oneshot::channel();
    if tx.send(McpRequest {
        command: command.to_string(),
        response_tx: resp_tx,
        blocked_commands: MCP_BLOCKED.to_vec(),
    }).await.is_err() {
        return "Error: sketch editor disconnected".to_string();
    }
    wake_gui(state);
    resp_rx.await.unwrap_or_else(|_| "Error: no response from sketch editor".to_string())
}

fn generate_session_id() -> String {
    use std::hash::{Hash, Hasher};
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    std::time::SystemTime::now().hash(&mut hasher);
    std::thread::current().id().hash(&mut hasher);
    format!("{:016x}", hasher.finish())
}

fn tool_result(id: Value, text: &str) -> Value {
    json!({
        "jsonrpc": "2.0", "id": id,
        "result": {
            "content": [{ "type": "text", "text": text }],
            "isError": false
        }
    })
}

fn tool_error(id: Value, text: &str) -> Value {
    json!({
        "jsonrpc": "2.0", "id": id,
        "result": {
            "content": [{ "type": "text", "text": text }],
            "isError": true
        }
    })
}

// ---------------------------------------------------------------------------
// Preamble sent before COMMANDS.md to AI agents on initialize
// ---------------------------------------------------------------------------

const MCP_PREAMBLE: &str = "You are controlling arael-sketch, a 2D CAD/drawing program, via MCP tools. \
You can create technical drawings by building geometry and constraining it. \
Use execute_command for single commands, execute_script for multi-line scripts \
(# comments and blank lines are skipped), and get_sketch_state to inspect the current sketch.";