- app/: app demo (Kivy/buildozer) da API do robo - software/: servidor RoboMovel (SLAM, profundidade, navegacao) e firmware da base movel (movido de firmware/) - simulacao/: projeto Godot do robo e script de lancamento servidor + simulacao - modelos/: planta do 5o andar do bloco D, cena Blender do corredor e modelo real do robo - partes/mdf/: chapas identificadas e nesting da base do robo (DXF/SVG/FCStd) - projeto/render, relatorio/imagens, slides: renders finais e apresentacoes de progresso - ferramentas/scripts: encaixe de chapas, planta do bloco D e render do corredor no Blender - .gitignore: ignora backups do Blender/FreeCAD e .godot/ Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
196 lines
6.0 KiB
GDScript
196 lines
6.0 KiB
GDScript
## Minimal MCP server over the Streamable HTTP transport: one JSON-RPC
|
|
## request per HTTP POST, plain "application/json" responses (no SSE,
|
|
## no session id — this is a local, single-client dev tool). Bound to
|
|
## 127.0.0.1 only. Exposes this project's file tools so Claude Code (or
|
|
## any other MCP client) can read/list/write files through the editor:
|
|
##
|
|
## claude mcp add --transport http claude-godot http://127.0.0.1:8787/mcp
|
|
class_name ClaudeGodotMcpServer
|
|
extends Node
|
|
|
|
signal listening(port: int)
|
|
signal stopped
|
|
signal client_tool_call(tool_name: String, input: Dictionary)
|
|
signal client_tool_result(tool_name: String, result: Dictionary)
|
|
|
|
const PROTOCOL_VERSION := "2025-06-18"
|
|
|
|
var port: int = 8787
|
|
|
|
var _tcp_server: TCPServer
|
|
var _peers: Array = [] # [{peer: StreamPeerTCP, buffer: PackedByteArray}]
|
|
|
|
func start(listen_port: int) -> Dictionary:
|
|
stop()
|
|
port = listen_port
|
|
_tcp_server = TCPServer.new()
|
|
var err := _tcp_server.listen(port, "127.0.0.1")
|
|
if err != OK:
|
|
_tcp_server = null
|
|
return {"error": "Could not listen on 127.0.0.1:%d (error code %d)." % [port, err]}
|
|
set_process(true)
|
|
listening.emit(port)
|
|
return {}
|
|
|
|
func stop() -> void:
|
|
if _tcp_server:
|
|
_tcp_server.stop()
|
|
_tcp_server = null
|
|
_peers.clear()
|
|
set_process(false)
|
|
stopped.emit()
|
|
|
|
func is_running() -> bool:
|
|
return _tcp_server != null
|
|
|
|
func _exit_tree() -> void:
|
|
stop()
|
|
|
|
func _process(_delta: float) -> void:
|
|
if _tcp_server == null:
|
|
return
|
|
|
|
while _tcp_server.is_connection_available():
|
|
_peers.append({"peer": _tcp_server.take_connection(), "buffer": PackedByteArray()})
|
|
|
|
var i := _peers.size() - 1
|
|
while i >= 0:
|
|
var entry: Dictionary = _peers[i]
|
|
var peer: StreamPeerTCP = entry["peer"]
|
|
peer.poll()
|
|
|
|
if peer.get_status() != StreamPeerTCP.STATUS_CONNECTED:
|
|
_peers.remove_at(i)
|
|
i -= 1
|
|
continue
|
|
|
|
var avail := peer.get_available_bytes()
|
|
if avail > 0:
|
|
var chunk := peer.get_data(avail)
|
|
if chunk[0] == OK:
|
|
var buf: PackedByteArray = entry["buffer"]
|
|
buf.append_array(chunk[1])
|
|
entry["buffer"] = buf
|
|
|
|
var request := _try_parse_http(entry["buffer"])
|
|
if not request.is_empty():
|
|
_handle_request(peer, request)
|
|
peer.disconnect_from_host()
|
|
_peers.remove_at(i)
|
|
i -= 1
|
|
|
|
static func _try_parse_http(buffer: PackedByteArray) -> Dictionary:
|
|
var text := buffer.get_string_from_utf8()
|
|
var header_end := text.find("\r\n\r\n")
|
|
if header_end == -1:
|
|
return {}
|
|
|
|
var lines := text.substr(0, header_end).split("\r\n")
|
|
if lines.is_empty():
|
|
return {}
|
|
var request_line := lines[0].split(" ")
|
|
if request_line.size() < 2:
|
|
return {"method": "INVALID", "path": "", "headers": {}, "body": PackedByteArray()}
|
|
|
|
var headers := {}
|
|
for i in range(1, lines.size()):
|
|
var colon := lines[i].find(":")
|
|
if colon == -1:
|
|
continue
|
|
headers[lines[i].substr(0, colon).strip_edges().to_lower()] = lines[i].substr(colon + 1).strip_edges()
|
|
|
|
var content_length := int(headers.get("content-length", "0"))
|
|
var body_start := header_end + 4
|
|
if buffer.size() - body_start < content_length:
|
|
return {} # wait for the rest of the body
|
|
|
|
return {
|
|
"method": request_line[0],
|
|
"path": request_line[1],
|
|
"headers": headers,
|
|
"body": buffer.slice(body_start, body_start + content_length),
|
|
}
|
|
|
|
static func _send_http(peer: StreamPeerTCP, status: int, status_text: String, body: String) -> void:
|
|
var body_bytes := body.to_utf8_buffer()
|
|
var head := "HTTP/1.1 %d %s\r\n" % [status, status_text]
|
|
head += "Content-Type: application/json\r\n"
|
|
head += "Content-Length: %d\r\n" % body_bytes.size()
|
|
head += "Connection: close\r\n\r\n"
|
|
peer.put_data(head.to_utf8_buffer())
|
|
if body_bytes.size() > 0:
|
|
peer.put_data(body_bytes)
|
|
|
|
func _handle_request(peer: StreamPeerTCP, req: Dictionary) -> void:
|
|
if req.get("method") != "POST":
|
|
_send_http(peer, 405, "Method Not Allowed", JSON.stringify({"error": "This endpoint only accepts POST JSON-RPC requests."}))
|
|
return
|
|
|
|
var json := JSON.new()
|
|
if json.parse((req["body"] as PackedByteArray).get_string_from_utf8()) != OK:
|
|
_send_http(peer, 400, "Bad Request", JSON.stringify({"jsonrpc": "2.0", "id": null, "error": {"code": -32700, "message": "Parse error"}}))
|
|
return
|
|
|
|
var rpc = json.get_data()
|
|
if not (rpc is Dictionary):
|
|
_send_http(peer, 400, "Bad Request", JSON.stringify({"jsonrpc": "2.0", "id": null, "error": {"code": -32600, "message": "Invalid request"}}))
|
|
return
|
|
|
|
var response := _dispatch(rpc)
|
|
if response.is_empty():
|
|
_send_http(peer, 202, "Accepted", "")
|
|
else:
|
|
_send_http(peer, 200, "OK", JSON.stringify(response))
|
|
|
|
## Returns {} for JSON-RPC notifications (no "id"), otherwise a full response envelope.
|
|
func _dispatch(rpc: Dictionary) -> Dictionary:
|
|
var method: String = rpc.get("method", "")
|
|
var has_id := rpc.has("id")
|
|
var id = rpc.get("id", null)
|
|
var params: Dictionary = rpc.get("params", {})
|
|
|
|
var result
|
|
var error_obj = null
|
|
|
|
match method:
|
|
"initialize":
|
|
result = {
|
|
"protocolVersion": PROTOCOL_VERSION,
|
|
"capabilities": {"tools": {}},
|
|
"serverInfo": {"name": "claude-godot", "version": "0.1.0"},
|
|
}
|
|
"notifications/initialized", "notifications/cancelled":
|
|
return {}
|
|
"ping":
|
|
result = {}
|
|
"tools/list":
|
|
result = {"tools": _mcp_tool_defs()}
|
|
"tools/call":
|
|
var tool_name: String = params.get("name", "")
|
|
var args: Dictionary = params.get("arguments", {})
|
|
client_tool_call.emit(tool_name, args)
|
|
var tr := ClaudeGodotTools.execute(tool_name, args)
|
|
client_tool_result.emit(tool_name, tr)
|
|
result = {
|
|
"content": [{"type": "text", "text": str(tr.get("content", ""))}],
|
|
"isError": tr.get("is_error", false),
|
|
}
|
|
_:
|
|
error_obj = {"code": -32601, "message": "Method not found: %s" % method}
|
|
|
|
if not has_id:
|
|
return {}
|
|
if error_obj != null:
|
|
return {"jsonrpc": "2.0", "id": id, "error": error_obj}
|
|
return {"jsonrpc": "2.0", "id": id, "result": result}
|
|
|
|
static func _mcp_tool_defs() -> Array:
|
|
var defs: Array = []
|
|
for t in ClaudeGodotTools.get_tool_definitions():
|
|
defs.append({
|
|
"name": t["name"],
|
|
"description": t.get("description", ""),
|
|
"inputSchema": t.get("input_schema", {"type": "object", "properties": {}}),
|
|
})
|
|
return defs
|