godot-agent-tools-mcp 0.2.1 → 0.3.0
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.
- package/package.json +1 -1
- package/server.mjs +509 -6
package/package.json
CHANGED
package/server.mjs
CHANGED
|
@@ -7,13 +7,67 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
|
|
|
7
7
|
import {
|
|
8
8
|
CallToolRequestSchema,
|
|
9
9
|
ListToolsRequestSchema,
|
|
10
|
+
ListResourcesRequestSchema,
|
|
11
|
+
ReadResourceRequestSchema,
|
|
10
12
|
} from "@modelcontextprotocol/sdk/types.js";
|
|
11
13
|
import net from "node:net";
|
|
14
|
+
import fs from "node:fs";
|
|
15
|
+
import path from "node:path";
|
|
16
|
+
import os from "node:os";
|
|
12
17
|
|
|
13
18
|
const HOST = process.env.GODOT_AGENT_HOST || "127.0.0.1";
|
|
14
|
-
|
|
19
|
+
// GODOT_AGENT_PORT env var forces a specific target, bypassing session discovery.
|
|
20
|
+
// Leave unset to use the multi-session registry.
|
|
21
|
+
const FORCED_PORT = process.env.GODOT_AGENT_PORT ? parseInt(process.env.GODOT_AGENT_PORT, 10) : null;
|
|
15
22
|
const TIMEOUT_MS = parseInt(process.env.GODOT_AGENT_TIMEOUT_MS || "15000", 10);
|
|
16
23
|
|
|
24
|
+
// Multi-editor session registry — matches the plugin-side writer at
|
|
25
|
+
// <home>/.godot-agent-tools/sessions/<pid>.json. Each file describes one
|
|
26
|
+
// running Godot editor with the plugin enabled.
|
|
27
|
+
const SESSION_DIR = path.join(os.homedir(), ".godot-agent-tools", "sessions");
|
|
28
|
+
|
|
29
|
+
function isProcessAlive(pid) {
|
|
30
|
+
try {
|
|
31
|
+
// signal 0 doesn't send anything — just tests whether we can signal the pid.
|
|
32
|
+
process.kill(pid, 0);
|
|
33
|
+
return true;
|
|
34
|
+
} catch (e) {
|
|
35
|
+
// EPERM means the process exists but we can't signal it (still "alive" for us).
|
|
36
|
+
return e.code === "EPERM";
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function listSessions() {
|
|
41
|
+
if (!fs.existsSync(SESSION_DIR)) return [];
|
|
42
|
+
let entries;
|
|
43
|
+
try { entries = fs.readdirSync(SESSION_DIR); }
|
|
44
|
+
catch { return []; }
|
|
45
|
+
const out = [];
|
|
46
|
+
for (const name of entries) {
|
|
47
|
+
if (!name.endsWith(".json")) continue;
|
|
48
|
+
const full = path.join(SESSION_DIR, name);
|
|
49
|
+
let parsed;
|
|
50
|
+
try { parsed = JSON.parse(fs.readFileSync(full, "utf8")); }
|
|
51
|
+
catch { continue; }
|
|
52
|
+
if (!parsed.pid || !parsed.port) continue;
|
|
53
|
+
const alive = isProcessAlive(parsed.pid);
|
|
54
|
+
if (!alive) {
|
|
55
|
+
// Clean up a stale entry opportunistically — plugin _exit_tree didn't fire
|
|
56
|
+
// (probably editor crashed or was killed).
|
|
57
|
+
try { fs.unlinkSync(full); } catch {}
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
out.push(parsed);
|
|
61
|
+
}
|
|
62
|
+
// Most recently started first.
|
|
63
|
+
out.sort((a, b) => (b.started_at_unix || 0) - (a.started_at_unix || 0));
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Tracks which session the shim is currently forwarding tool calls to. Starts
|
|
68
|
+
// unset; first call resolves via listSessions() or FORCED_PORT.
|
|
69
|
+
let activeSessionPid = null;
|
|
70
|
+
|
|
17
71
|
const TOOLS = [
|
|
18
72
|
{
|
|
19
73
|
name: "scene_inspect",
|
|
@@ -302,6 +356,32 @@ const TOOLS = [
|
|
|
302
356
|
},
|
|
303
357
|
},
|
|
304
358
|
},
|
|
359
|
+
{
|
|
360
|
+
name: "script_patch",
|
|
361
|
+
method: "script.patch",
|
|
362
|
+
description:
|
|
363
|
+
"Apply targeted edits to an existing .gd file. Two modes: 'replacements' is an array of {old, new} where each 'old' must match exactly once in the file (ambiguous or missing matches return a clean error instead of silently mangling); 'full_source' overwrites the whole file. After writing, the tool parse-checks the result via ResourceLoader.load; if parsing fails the original is restored and an error is returned. Supports dry_run.",
|
|
364
|
+
inputSchema: {
|
|
365
|
+
type: "object",
|
|
366
|
+
required: ["path"],
|
|
367
|
+
properties: {
|
|
368
|
+
path: { type: "string", description: "Target .gd file." },
|
|
369
|
+
replacements: {
|
|
370
|
+
type: "array",
|
|
371
|
+
items: {
|
|
372
|
+
type: "object",
|
|
373
|
+
required: ["old", "new"],
|
|
374
|
+
properties: {
|
|
375
|
+
old: { type: "string" },
|
|
376
|
+
new: { type: "string" },
|
|
377
|
+
},
|
|
378
|
+
},
|
|
379
|
+
},
|
|
380
|
+
full_source: { type: "string", description: "Alternative to replacements: overwrite with this full source." },
|
|
381
|
+
dry_run: { type: "boolean", default: false },
|
|
382
|
+
},
|
|
383
|
+
},
|
|
384
|
+
},
|
|
305
385
|
{
|
|
306
386
|
name: "script_create",
|
|
307
387
|
method: "script.create",
|
|
@@ -489,6 +569,247 @@ const TOOLS = [
|
|
|
489
569
|
description: "List all registered autoloads with their paths and singleton flags.",
|
|
490
570
|
inputSchema: { type: "object", properties: {} },
|
|
491
571
|
},
|
|
572
|
+
{
|
|
573
|
+
name: "editor_state",
|
|
574
|
+
method: "editor.state",
|
|
575
|
+
description:
|
|
576
|
+
"Consolidated editor + project status in one call: Godot version, project name, current scene (path/class/root_name/open), list of open scenes, is-playing flag, playing scene path.",
|
|
577
|
+
inputSchema: { type: "object", properties: {} },
|
|
578
|
+
},
|
|
579
|
+
{
|
|
580
|
+
name: "editor_selection_get",
|
|
581
|
+
method: "editor.selection_get",
|
|
582
|
+
description: "Return the currently-selected nodes in the editor tree dock — for 'operate on what I clicked' workflows.",
|
|
583
|
+
inputSchema: { type: "object", properties: {} },
|
|
584
|
+
},
|
|
585
|
+
{
|
|
586
|
+
name: "editor_selection_set",
|
|
587
|
+
method: "editor.selection_set",
|
|
588
|
+
description: "Select specific nodes in the editor tree dock. Useful after an agent operation to point the user's attention at the result.",
|
|
589
|
+
inputSchema: {
|
|
590
|
+
type: "object",
|
|
591
|
+
required: ["node_paths"],
|
|
592
|
+
properties: {
|
|
593
|
+
node_paths: { type: "array", items: { type: "string" }, description: "NodePaths relative to the scene root. '.' = root." },
|
|
594
|
+
},
|
|
595
|
+
},
|
|
596
|
+
},
|
|
597
|
+
{
|
|
598
|
+
name: "editor_game_screenshot",
|
|
599
|
+
method: "editor.game_screenshot",
|
|
600
|
+
description:
|
|
601
|
+
"Capture the viewport of the CURRENTLY RUNNING game (after user pressed F5 etc.). Works via the _MCPGameBridge autoload registered by this plugin. If no game is running, returns an error pointing the user to run.scene_headless as the subprocess-based alternative.",
|
|
602
|
+
inputSchema: {
|
|
603
|
+
type: "object",
|
|
604
|
+
properties: {
|
|
605
|
+
output: { type: "string", default: "res://.godot/agent_tools/game_screenshot.png" },
|
|
606
|
+
timeout_ms: { type: "integer", default: 5000 },
|
|
607
|
+
},
|
|
608
|
+
},
|
|
609
|
+
},
|
|
610
|
+
{
|
|
611
|
+
name: "logs_read",
|
|
612
|
+
method: "logs.read",
|
|
613
|
+
description:
|
|
614
|
+
"Read print / push_error / push_warning output from the currently running game (captured by the _MCPGameBridge autoload). Entries include level, message, and timestamp. Returns an empty buffer with a helpful note if the game isn't running.",
|
|
615
|
+
inputSchema: {
|
|
616
|
+
type: "object",
|
|
617
|
+
properties: {
|
|
618
|
+
clear: { type: "boolean", default: false, description: "Clear the buffer after reading." },
|
|
619
|
+
max_lines: { type: "integer", default: 200, description: "Cap on entries returned; older entries are omitted first." },
|
|
620
|
+
},
|
|
621
|
+
},
|
|
622
|
+
},
|
|
623
|
+
{
|
|
624
|
+
name: "logs_clear",
|
|
625
|
+
method: "logs.clear",
|
|
626
|
+
description: "Drop the game log buffer. Safe to call whether the game is running or not.",
|
|
627
|
+
inputSchema: { type: "object", properties: {} },
|
|
628
|
+
},
|
|
629
|
+
{
|
|
630
|
+
name: "performance_monitors",
|
|
631
|
+
method: "performance.monitors",
|
|
632
|
+
description:
|
|
633
|
+
"Read Godot's Performance monitors (FPS, frame time, memory, object/node counts, draw calls, etc.). Default returns a common set; pass 'monitors' with specific names (fps, frame_time, mem_static, draw_calls, orphan_nodes, ...) for targeted reads.",
|
|
634
|
+
inputSchema: {
|
|
635
|
+
type: "object",
|
|
636
|
+
properties: {
|
|
637
|
+
monitors: {
|
|
638
|
+
type: "array",
|
|
639
|
+
items: { type: "string" },
|
|
640
|
+
description: "Optional subset of monitor names. Full list: fps, frame_time, physics_time, mem_static, mem_static_max, objects, resources, nodes, orphan_nodes, draw_calls, primitives, 2d_items, 2d_draw_calls, video_mem, audio_latency, physics_2d_active_objects, physics_3d_active_objects.",
|
|
641
|
+
},
|
|
642
|
+
},
|
|
643
|
+
},
|
|
644
|
+
},
|
|
645
|
+
{
|
|
646
|
+
name: "test_run",
|
|
647
|
+
method: "test.run",
|
|
648
|
+
description:
|
|
649
|
+
"Detect and run a GDScript test framework (GUT or GdUnit4), return structured results. Auto-detects the installed framework (via addons/gut or addons/gdUnit4), can be forced via 'framework'. Returns {total, passed, failed, skipped, failures: [{name, file, line, message}], raw_output}. Higher level than run.scene_headless — understands the framework's test concepts and summary format instead of asking you to parse stdout.",
|
|
650
|
+
inputSchema: {
|
|
651
|
+
type: "object",
|
|
652
|
+
properties: {
|
|
653
|
+
framework: { type: "string", enum: ["auto", "gut", "gdunit4"], default: "auto" },
|
|
654
|
+
directory: { type: "string", description: "Test directory (defaults to 'res://test')." },
|
|
655
|
+
pattern: { type: "string", description: "Filename pattern (framework-specific default)." },
|
|
656
|
+
timeout_seconds: { type: "integer", default: 60 },
|
|
657
|
+
},
|
|
658
|
+
},
|
|
659
|
+
},
|
|
660
|
+
{
|
|
661
|
+
name: "client_list",
|
|
662
|
+
method: "client.list",
|
|
663
|
+
description: "List supported MCP clients and whether each has the godot-agent-tools server configured. Shows the config file path for every client so users know where to look.",
|
|
664
|
+
inputSchema: { type: "object", properties: {} },
|
|
665
|
+
},
|
|
666
|
+
{
|
|
667
|
+
name: "client_configure",
|
|
668
|
+
method: "client.configure",
|
|
669
|
+
description:
|
|
670
|
+
"Write the godot-agent-tools MCP server entry into the specified client's config file. Idempotent (won't duplicate); pass overwrite:true to force-replace an existing entry. Supported clients: claude_code_project, claude_code_user, claude_desktop, cursor_project, cursor_user.",
|
|
671
|
+
inputSchema: {
|
|
672
|
+
type: "object",
|
|
673
|
+
required: ["client"],
|
|
674
|
+
properties: {
|
|
675
|
+
client: { type: "string", enum: ["claude_code_project", "claude_code_user", "claude_desktop", "cursor_project", "cursor_user"] },
|
|
676
|
+
overwrite: { type: "boolean", default: false },
|
|
677
|
+
},
|
|
678
|
+
},
|
|
679
|
+
},
|
|
680
|
+
{
|
|
681
|
+
name: "client_remove",
|
|
682
|
+
method: "client.remove",
|
|
683
|
+
description: "Remove the godot-agent-tools entry from the specified client's config.",
|
|
684
|
+
inputSchema: {
|
|
685
|
+
type: "object",
|
|
686
|
+
required: ["client"],
|
|
687
|
+
properties: {
|
|
688
|
+
client: { type: "string", enum: ["claude_code_project", "claude_code_user", "claude_desktop", "cursor_project", "cursor_user"] },
|
|
689
|
+
},
|
|
690
|
+
},
|
|
691
|
+
},
|
|
692
|
+
{
|
|
693
|
+
name: "physics_autofit_collision_shape_2d",
|
|
694
|
+
method: "physics.autofit_collision_shape_2d",
|
|
695
|
+
description:
|
|
696
|
+
"Compute a CollisionShape2D sized to a sibling Sprite2D/AnimatedSprite2D's visual bounds. Can auto-create the CollisionShape2D if it doesn't exist yet (pass create:true). Shape type: 'rectangle' (default), 'circle', or 'capsule'. Optional margin shrinks the shape.",
|
|
697
|
+
inputSchema: {
|
|
698
|
+
type: "object",
|
|
699
|
+
required: ["node_path"],
|
|
700
|
+
properties: {
|
|
701
|
+
node_path: { type: "string", description: "CollisionShape2D to fit. Created if missing + create:true." },
|
|
702
|
+
source: { type: "string", description: "NodePath to a Sprite2D/AnimatedSprite2D. Auto-detected among siblings if omitted." },
|
|
703
|
+
shape: { type: "string", enum: ["rectangle", "circle", "capsule"], default: "rectangle" },
|
|
704
|
+
margin: { type: "number", default: 0 },
|
|
705
|
+
create: { type: "boolean", default: false },
|
|
706
|
+
},
|
|
707
|
+
},
|
|
708
|
+
},
|
|
709
|
+
{
|
|
710
|
+
name: "theme_set_color",
|
|
711
|
+
method: "theme.set_color",
|
|
712
|
+
description: "Set a color entry in a Theme resource. Wraps Theme.set_color(item, type, color) — e.g. item='font_color', type='Label'.",
|
|
713
|
+
inputSchema: {
|
|
714
|
+
type: "object",
|
|
715
|
+
required: ["path", "item", "type", "color"],
|
|
716
|
+
properties: {
|
|
717
|
+
path: { type: "string", description: "Path to .tres Theme resource." },
|
|
718
|
+
item: { type: "string", description: "Theme item name (e.g. 'font_color', 'bg_color')." },
|
|
719
|
+
type: { type: "string", description: "Control class name (e.g. 'Button', 'Label')." },
|
|
720
|
+
color: { description: "[r,g,b(,a)] or '#hex'." },
|
|
721
|
+
},
|
|
722
|
+
},
|
|
723
|
+
},
|
|
724
|
+
{
|
|
725
|
+
name: "theme_set_constant",
|
|
726
|
+
method: "theme.set_constant",
|
|
727
|
+
description: "Set an int constant in a Theme resource. E.g. item='h_separation', type='HBoxContainer'.",
|
|
728
|
+
inputSchema: {
|
|
729
|
+
type: "object",
|
|
730
|
+
required: ["path", "item", "type", "value"],
|
|
731
|
+
properties: {
|
|
732
|
+
path: { type: "string" },
|
|
733
|
+
item: { type: "string" },
|
|
734
|
+
type: { type: "string" },
|
|
735
|
+
value: { type: "integer" },
|
|
736
|
+
},
|
|
737
|
+
},
|
|
738
|
+
},
|
|
739
|
+
{
|
|
740
|
+
name: "theme_set_font_size",
|
|
741
|
+
method: "theme.set_font_size",
|
|
742
|
+
description: "Set a font-size entry in a Theme resource. E.g. item='font_size', type='Label'.",
|
|
743
|
+
inputSchema: {
|
|
744
|
+
type: "object",
|
|
745
|
+
required: ["path", "item", "type", "value"],
|
|
746
|
+
properties: {
|
|
747
|
+
path: { type: "string" },
|
|
748
|
+
item: { type: "string" },
|
|
749
|
+
type: { type: "string" },
|
|
750
|
+
value: { type: "integer" },
|
|
751
|
+
},
|
|
752
|
+
},
|
|
753
|
+
},
|
|
754
|
+
{
|
|
755
|
+
name: "theme_set_stylebox_flat",
|
|
756
|
+
method: "theme.set_stylebox_flat",
|
|
757
|
+
description:
|
|
758
|
+
"Create (or replace) a StyleBoxFlat on a Theme with the given properties and assign it to theme.<item>.<type>. Saves the usual multi-step StyleBoxFlat setup — e.g. {item: 'normal', type: 'Button', properties: {bg_color: [0.1,0.1,0.12,1], corner_radius_top_left: 8, ...}}.",
|
|
759
|
+
inputSchema: {
|
|
760
|
+
type: "object",
|
|
761
|
+
required: ["path", "item", "type"],
|
|
762
|
+
properties: {
|
|
763
|
+
path: { type: "string" },
|
|
764
|
+
item: { type: "string" },
|
|
765
|
+
type: { type: "string" },
|
|
766
|
+
properties: { type: "object", additionalProperties: true },
|
|
767
|
+
},
|
|
768
|
+
},
|
|
769
|
+
},
|
|
770
|
+
{
|
|
771
|
+
name: "session_list",
|
|
772
|
+
method: "__local__.session_list",
|
|
773
|
+
description:
|
|
774
|
+
"List every running Godot editor with the Agent Tools plugin enabled (each becomes a separate 'session' the shim can target). Each entry: {pid, port, project_path, project_name, godot_version, started_at_unix, active}. The shim's default target is the most-recently-started session; use session_activate to pin a specific one.",
|
|
775
|
+
inputSchema: { type: "object", properties: {} },
|
|
776
|
+
},
|
|
777
|
+
{
|
|
778
|
+
name: "session_activate",
|
|
779
|
+
method: "__local__.session_activate",
|
|
780
|
+
description:
|
|
781
|
+
"Pin subsequent tool calls to a specific Godot editor session (by pid from session_list). Pass pid:null to clear the pin and fall back to 'most-recently-started'. Changing the active session tears down the existing TCP connection; the next call reconnects to the new target.",
|
|
782
|
+
inputSchema: {
|
|
783
|
+
type: "object",
|
|
784
|
+
properties: {
|
|
785
|
+
pid: { type: ["integer", "null"], description: "PID of the session to target; null to clear." },
|
|
786
|
+
},
|
|
787
|
+
},
|
|
788
|
+
},
|
|
789
|
+
{
|
|
790
|
+
name: "batch_execute",
|
|
791
|
+
method: "batch.execute",
|
|
792
|
+
description:
|
|
793
|
+
"Run multiple tool calls in one round trip. Each call is dispatched server-side and results are returned in order. Useful when you know the exact sequence you want — saves TCP round trips vs. parallel MCP calls.",
|
|
794
|
+
inputSchema: {
|
|
795
|
+
type: "object",
|
|
796
|
+
required: ["calls"],
|
|
797
|
+
properties: {
|
|
798
|
+
calls: {
|
|
799
|
+
type: "array",
|
|
800
|
+
items: {
|
|
801
|
+
type: "object",
|
|
802
|
+
required: ["method"],
|
|
803
|
+
properties: {
|
|
804
|
+
method: { type: "string", description: "Dotted method name (e.g. 'scene.add_node')." },
|
|
805
|
+
params: { type: "object" },
|
|
806
|
+
},
|
|
807
|
+
},
|
|
808
|
+
},
|
|
809
|
+
stop_on_error: { type: "boolean", default: false, description: "Halt the batch on the first failure. Default keeps going." },
|
|
810
|
+
},
|
|
811
|
+
},
|
|
812
|
+
},
|
|
492
813
|
{
|
|
493
814
|
name: "editor_reload_filesystem",
|
|
494
815
|
method: "editor.reload_filesystem",
|
|
@@ -674,6 +995,31 @@ const TOOLS = [
|
|
|
674
995
|
},
|
|
675
996
|
},
|
|
676
997
|
},
|
|
998
|
+
{
|
|
999
|
+
name: "fs_read_text",
|
|
1000
|
+
method: "fs.read_text",
|
|
1001
|
+
description: "Read a text file under res://. Complement to user_fs_read (which targets user:// for runtime-written state).",
|
|
1002
|
+
inputSchema: {
|
|
1003
|
+
type: "object",
|
|
1004
|
+
required: ["path"],
|
|
1005
|
+
properties: { path: { type: "string", description: "Must begin with 'res://'." } },
|
|
1006
|
+
},
|
|
1007
|
+
},
|
|
1008
|
+
{
|
|
1009
|
+
name: "fs_write_text",
|
|
1010
|
+
method: "fs.write_text",
|
|
1011
|
+
description:
|
|
1012
|
+
"Write a text file under res://. Creates parent directories if needed; triggers the editor's filesystem rescan so the new file shows up in the FileSystem dock immediately. For .gd scripts prefer script_create / script_patch — those run a parse check.",
|
|
1013
|
+
inputSchema: {
|
|
1014
|
+
type: "object",
|
|
1015
|
+
required: ["path", "content"],
|
|
1016
|
+
properties: {
|
|
1017
|
+
path: { type: "string" },
|
|
1018
|
+
content: { type: "string" },
|
|
1019
|
+
overwrite: { type: "boolean", default: false },
|
|
1020
|
+
},
|
|
1021
|
+
},
|
|
1022
|
+
},
|
|
677
1023
|
{
|
|
678
1024
|
name: "user_fs_read",
|
|
679
1025
|
method: "user_fs.read",
|
|
@@ -791,10 +1137,12 @@ const BY_NAME = Object.fromEntries(TOOLS.map((t) => [t.name, t]));
|
|
|
791
1137
|
|
|
792
1138
|
// Persistent Godot TCP client. One socket reused across tool calls; outstanding
|
|
793
1139
|
// requests are tracked by id so multiple in-flight calls don't interleave data.
|
|
1140
|
+
// Target port is resolved lazily per call so session.activate / session death
|
|
1141
|
+
// switch over without proactive teardown.
|
|
794
1142
|
class GodotClient {
|
|
795
|
-
constructor(host
|
|
1143
|
+
constructor(host) {
|
|
796
1144
|
this.host = host;
|
|
797
|
-
this.port =
|
|
1145
|
+
this.port = null;
|
|
798
1146
|
this.socket = null;
|
|
799
1147
|
this.buffer = "";
|
|
800
1148
|
this.pending = new Map(); // id -> { resolve, reject, timer }
|
|
@@ -802,9 +1150,46 @@ class GodotClient {
|
|
|
802
1150
|
this.connecting = null;
|
|
803
1151
|
}
|
|
804
1152
|
|
|
1153
|
+
// Priority: GODOT_AGENT_PORT env > pinned active session > most recent session.
|
|
1154
|
+
_resolvePort() {
|
|
1155
|
+
if (FORCED_PORT != null) return FORCED_PORT;
|
|
1156
|
+
const sessions = listSessions();
|
|
1157
|
+
if (sessions.length === 0) return null;
|
|
1158
|
+
if (activeSessionPid != null) {
|
|
1159
|
+
const pinned = sessions.find((s) => s.pid === activeSessionPid);
|
|
1160
|
+
if (pinned) return pinned.port;
|
|
1161
|
+
activeSessionPid = null; // pinned session died — fall back
|
|
1162
|
+
}
|
|
1163
|
+
return sessions[0].port;
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
// If the active session changed, drop the old socket so the next call
|
|
1167
|
+
// reconnects to the new target.
|
|
1168
|
+
_maybeResetForPortChange() {
|
|
1169
|
+
const target = this._resolvePort();
|
|
1170
|
+
if (target !== this.port && this.socket) {
|
|
1171
|
+
try { this.socket.destroy(); } catch {}
|
|
1172
|
+
this.socket = null;
|
|
1173
|
+
this.buffer = "";
|
|
1174
|
+
for (const { reject, timer } of this.pending.values()) {
|
|
1175
|
+
clearTimeout(timer);
|
|
1176
|
+
reject(new Error("session target changed mid-flight"));
|
|
1177
|
+
}
|
|
1178
|
+
this.pending.clear();
|
|
1179
|
+
}
|
|
1180
|
+
this.port = target;
|
|
1181
|
+
}
|
|
1182
|
+
|
|
805
1183
|
async _ensureConnected() {
|
|
1184
|
+
this._maybeResetForPortChange();
|
|
806
1185
|
if (this.socket && !this.socket.destroyed) return;
|
|
807
1186
|
if (this.connecting) return this.connecting;
|
|
1187
|
+
if (this.port == null) {
|
|
1188
|
+
throw new Error(
|
|
1189
|
+
"No Godot editor session found. Open a project with the 'Agent Tools' plugin enabled, " +
|
|
1190
|
+
"or set the GODOT_AGENT_PORT env var to target a specific port."
|
|
1191
|
+
);
|
|
1192
|
+
}
|
|
808
1193
|
|
|
809
1194
|
this.connecting = new Promise((resolve, reject) => {
|
|
810
1195
|
const s = new net.Socket();
|
|
@@ -921,13 +1306,62 @@ class GodotClient {
|
|
|
921
1306
|
}
|
|
922
1307
|
}
|
|
923
1308
|
|
|
924
|
-
const client = new GodotClient(HOST
|
|
1309
|
+
const client = new GodotClient(HOST);
|
|
925
1310
|
|
|
926
1311
|
const server = new Server(
|
|
927
|
-
{ name: "godot-agent-tools", version: "0.
|
|
928
|
-
{ capabilities: { tools: {} } }
|
|
1312
|
+
{ name: "godot-agent-tools", version: "0.3.0" },
|
|
1313
|
+
{ capabilities: { tools: {}, resources: {} } }
|
|
929
1314
|
);
|
|
930
1315
|
|
|
1316
|
+
// MCP Resources — subscribable read-only endpoints. Agents that support
|
|
1317
|
+
// resources can 'watch' these without repeatedly calling tools.
|
|
1318
|
+
const RESOURCES = [
|
|
1319
|
+
{
|
|
1320
|
+
uri: "godot://editor/state",
|
|
1321
|
+
name: "Editor state",
|
|
1322
|
+
description: "Current editor state: Godot version, project name, current scene, playing status.",
|
|
1323
|
+
mimeType: "application/json",
|
|
1324
|
+
method: "editor.state",
|
|
1325
|
+
},
|
|
1326
|
+
{
|
|
1327
|
+
uri: "godot://scene/current",
|
|
1328
|
+
name: "Current scene",
|
|
1329
|
+
description: "Currently-edited scene (path, root name, root class, open?).",
|
|
1330
|
+
mimeType: "application/json",
|
|
1331
|
+
method: "scene.current",
|
|
1332
|
+
},
|
|
1333
|
+
{
|
|
1334
|
+
uri: "godot://scene/hierarchy",
|
|
1335
|
+
name: "Current scene hierarchy",
|
|
1336
|
+
description: "Full tree of the currently-edited scene.",
|
|
1337
|
+
mimeType: "application/json",
|
|
1338
|
+
method: "scene.inspect",
|
|
1339
|
+
},
|
|
1340
|
+
{
|
|
1341
|
+
uri: "godot://selection/current",
|
|
1342
|
+
name: "Editor selection",
|
|
1343
|
+
description: "Nodes currently selected in the editor tree dock.",
|
|
1344
|
+
mimeType: "application/json",
|
|
1345
|
+
method: "editor.selection_get",
|
|
1346
|
+
},
|
|
1347
|
+
{
|
|
1348
|
+
uri: "godot://logs/recent",
|
|
1349
|
+
name: "Recent game logs",
|
|
1350
|
+
description: "Recent print / push_error / push_warning output from the running game.",
|
|
1351
|
+
mimeType: "application/json",
|
|
1352
|
+
method: "logs.read",
|
|
1353
|
+
},
|
|
1354
|
+
{
|
|
1355
|
+
uri: "godot://performance/monitors",
|
|
1356
|
+
name: "Performance monitors",
|
|
1357
|
+
description: "FPS, frame time, memory, draw calls, object counts.",
|
|
1358
|
+
mimeType: "application/json",
|
|
1359
|
+
method: "performance.monitors",
|
|
1360
|
+
},
|
|
1361
|
+
];
|
|
1362
|
+
|
|
1363
|
+
const RESOURCE_BY_URI = Object.fromEntries(RESOURCES.map((r) => [r.uri, r]));
|
|
1364
|
+
|
|
931
1365
|
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
932
1366
|
tools: TOOLS.map(({ name, description, inputSchema }) => ({
|
|
933
1367
|
name,
|
|
@@ -944,6 +1378,37 @@ server.setRequestHandler(CallToolRequestSchema, async (req) => {
|
|
|
944
1378
|
content: [{ type: "text", text: `Unknown tool: ${req.params.name}` }],
|
|
945
1379
|
};
|
|
946
1380
|
}
|
|
1381
|
+
|
|
1382
|
+
// session_list / session_activate are shim-local — they manage the MCP shim's
|
|
1383
|
+
// own routing state and don't forward to any Godot process.
|
|
1384
|
+
if (tool.method === "__local__.session_list") {
|
|
1385
|
+
const sessions = listSessions().map((s) => ({
|
|
1386
|
+
...s,
|
|
1387
|
+
active: activeSessionPid != null ? s.pid === activeSessionPid : s === listSessions()[0],
|
|
1388
|
+
}));
|
|
1389
|
+
return {
|
|
1390
|
+
content: [{ type: "text", text: JSON.stringify({ sessions, count: sessions.length, active_pid: activeSessionPid }, null, 2) }],
|
|
1391
|
+
};
|
|
1392
|
+
}
|
|
1393
|
+
if (tool.method === "__local__.session_activate") {
|
|
1394
|
+
const pid = req.params.arguments?.pid ?? null;
|
|
1395
|
+
if (pid === null) {
|
|
1396
|
+
activeSessionPid = null;
|
|
1397
|
+
} else {
|
|
1398
|
+
const sessions = listSessions();
|
|
1399
|
+
if (!sessions.some((s) => s.pid === pid)) {
|
|
1400
|
+
return {
|
|
1401
|
+
isError: true,
|
|
1402
|
+
content: [{ type: "text", text: `No active session with pid=${pid}. Call session_list to see candidates.` }],
|
|
1403
|
+
};
|
|
1404
|
+
}
|
|
1405
|
+
activeSessionPid = pid;
|
|
1406
|
+
}
|
|
1407
|
+
return {
|
|
1408
|
+
content: [{ type: "text", text: JSON.stringify({ active_pid: activeSessionPid }, null, 2) }],
|
|
1409
|
+
};
|
|
1410
|
+
}
|
|
1411
|
+
|
|
947
1412
|
try {
|
|
948
1413
|
const result = await client.call(tool.method, req.params.arguments || {});
|
|
949
1414
|
return {
|
|
@@ -957,5 +1422,43 @@ server.setRequestHandler(CallToolRequestSchema, async (req) => {
|
|
|
957
1422
|
}
|
|
958
1423
|
});
|
|
959
1424
|
|
|
1425
|
+
server.setRequestHandler(ListResourcesRequestSchema, async () => ({
|
|
1426
|
+
resources: RESOURCES.map(({ uri, name, description, mimeType }) => ({
|
|
1427
|
+
uri,
|
|
1428
|
+
name,
|
|
1429
|
+
description,
|
|
1430
|
+
mimeType,
|
|
1431
|
+
})),
|
|
1432
|
+
}));
|
|
1433
|
+
|
|
1434
|
+
server.setRequestHandler(ReadResourceRequestSchema, async (req) => {
|
|
1435
|
+
const resource = RESOURCE_BY_URI[req.params.uri];
|
|
1436
|
+
if (!resource) {
|
|
1437
|
+
throw new Error(`Unknown resource URI: ${req.params.uri}`);
|
|
1438
|
+
}
|
|
1439
|
+
try {
|
|
1440
|
+
const result = await client.call(resource.method, {});
|
|
1441
|
+
return {
|
|
1442
|
+
contents: [
|
|
1443
|
+
{
|
|
1444
|
+
uri: resource.uri,
|
|
1445
|
+
mimeType: resource.mimeType,
|
|
1446
|
+
text: JSON.stringify(result, null, 2),
|
|
1447
|
+
},
|
|
1448
|
+
],
|
|
1449
|
+
};
|
|
1450
|
+
} catch (e) {
|
|
1451
|
+
return {
|
|
1452
|
+
contents: [
|
|
1453
|
+
{
|
|
1454
|
+
uri: resource.uri,
|
|
1455
|
+
mimeType: resource.mimeType,
|
|
1456
|
+
text: JSON.stringify({ error: e.message }, null, 2),
|
|
1457
|
+
},
|
|
1458
|
+
],
|
|
1459
|
+
};
|
|
1460
|
+
}
|
|
1461
|
+
});
|
|
1462
|
+
|
|
960
1463
|
const transport = new StdioServerTransport();
|
|
961
1464
|
await server.connect(transport);
|