unreal-engine-mcp-server 0.5.4 → 0.5.5

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.
Files changed (468) hide show
  1. package/dist/automation/bridge.d.ts.map +1 -0
  2. package/dist/automation/bridge.js.map +1 -0
  3. package/dist/automation/connection-manager.d.ts.map +1 -0
  4. package/dist/automation/connection-manager.js.map +1 -0
  5. package/dist/automation/handshake.d.ts.map +1 -0
  6. package/dist/automation/handshake.js.map +1 -0
  7. package/dist/automation/index.d.ts.map +1 -0
  8. package/dist/automation/index.js.map +1 -0
  9. package/dist/automation/message-handler.d.ts.map +1 -0
  10. package/dist/automation/message-handler.js.map +1 -0
  11. package/dist/automation/request-tracker.d.ts.map +1 -0
  12. package/dist/automation/request-tracker.js.map +1 -0
  13. package/dist/automation/types.d.ts.map +1 -0
  14. package/dist/automation/types.js.map +1 -0
  15. package/dist/cli.d.ts.map +1 -0
  16. package/dist/cli.js +4 -3
  17. package/dist/cli.js.map +1 -0
  18. package/dist/config/class-aliases.d.ts.map +1 -0
  19. package/dist/config/class-aliases.js.map +1 -0
  20. package/dist/config.d.ts.map +1 -0
  21. package/dist/config.js.map +1 -0
  22. package/dist/constants.d.ts.map +1 -0
  23. package/dist/constants.js.map +1 -0
  24. package/dist/graphql/loaders.d.ts.map +1 -0
  25. package/dist/graphql/loaders.js.map +1 -0
  26. package/dist/graphql/resolvers.d.ts.map +1 -0
  27. package/dist/graphql/resolvers.js +29 -29
  28. package/dist/graphql/resolvers.js.map +1 -0
  29. package/dist/graphql/schema.d.ts.map +1 -0
  30. package/dist/graphql/schema.js.map +1 -0
  31. package/dist/graphql/server.d.ts.map +1 -0
  32. package/dist/graphql/server.js.map +1 -0
  33. package/dist/graphql/types.d.ts.map +1 -0
  34. package/dist/graphql/types.js.map +1 -0
  35. package/dist/handlers/resource-handlers.d.ts.map +1 -0
  36. package/dist/handlers/resource-handlers.js.map +1 -0
  37. package/dist/index.d.ts +1 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +64 -7
  40. package/dist/index.js.map +1 -0
  41. package/dist/resources/actors.d.ts.map +1 -0
  42. package/dist/resources/actors.js.map +1 -0
  43. package/dist/resources/assets.d.ts.map +1 -0
  44. package/dist/resources/assets.js +6 -4
  45. package/dist/resources/assets.js.map +1 -0
  46. package/dist/resources/levels.d.ts.map +1 -0
  47. package/dist/resources/levels.js.map +1 -0
  48. package/dist/server/resource-registry.d.ts.map +1 -0
  49. package/dist/server/resource-registry.js.map +1 -0
  50. package/dist/server/tool-registry.d.ts.map +1 -0
  51. package/dist/server/tool-registry.js.map +1 -0
  52. package/dist/server-setup.d.ts.map +1 -0
  53. package/dist/server-setup.js.map +1 -0
  54. package/dist/services/health-monitor.d.ts.map +1 -0
  55. package/dist/services/health-monitor.js.map +1 -0
  56. package/dist/services/metrics-server.d.ts.map +1 -0
  57. package/dist/services/metrics-server.js.map +1 -0
  58. package/dist/tools/actors.d.ts.map +1 -0
  59. package/dist/tools/actors.js +3 -1
  60. package/dist/tools/actors.js.map +1 -0
  61. package/dist/tools/animation.d.ts.map +1 -0
  62. package/dist/tools/animation.js +2 -2
  63. package/dist/tools/animation.js.map +1 -0
  64. package/dist/tools/assets.d.ts.map +1 -0
  65. package/dist/tools/assets.js.map +1 -0
  66. package/dist/tools/audio.d.ts.map +1 -0
  67. package/dist/tools/audio.js.map +1 -0
  68. package/dist/tools/base-tool.d.ts.map +1 -0
  69. package/dist/tools/base-tool.js.map +1 -0
  70. package/dist/tools/behavior-tree.d.ts.map +1 -0
  71. package/dist/tools/behavior-tree.js.map +1 -0
  72. package/dist/tools/blueprint.d.ts.map +1 -0
  73. package/dist/tools/blueprint.js +4 -2
  74. package/dist/tools/blueprint.js.map +1 -0
  75. package/dist/tools/consolidated-tool-definitions.d.ts.map +1 -0
  76. package/dist/tools/consolidated-tool-definitions.js.map +1 -0
  77. package/dist/tools/consolidated-tool-handlers.d.ts.map +1 -0
  78. package/dist/tools/consolidated-tool-handlers.js.map +1 -0
  79. package/dist/tools/debug.d.ts.map +1 -0
  80. package/dist/tools/debug.js +3 -1
  81. package/dist/tools/debug.js.map +1 -0
  82. package/dist/tools/dynamic-handler-registry.d.ts.map +1 -0
  83. package/dist/tools/dynamic-handler-registry.js +3 -1
  84. package/dist/tools/dynamic-handler-registry.js.map +1 -0
  85. package/dist/tools/editor.d.ts.map +1 -0
  86. package/dist/tools/editor.js +1 -1
  87. package/dist/tools/editor.js.map +1 -0
  88. package/dist/tools/engine.d.ts.map +1 -0
  89. package/dist/tools/engine.js.map +1 -0
  90. package/dist/tools/environment.d.ts.map +1 -0
  91. package/dist/tools/environment.js +2 -2
  92. package/dist/tools/environment.js.map +1 -0
  93. package/dist/tools/foliage.d.ts.map +1 -0
  94. package/dist/tools/foliage.js.map +1 -0
  95. package/dist/tools/handlers/actor-handlers.d.ts +1 -1
  96. package/dist/tools/handlers/actor-handlers.d.ts.map +1 -0
  97. package/dist/tools/handlers/actor-handlers.js +6 -5
  98. package/dist/tools/handlers/actor-handlers.js.map +1 -0
  99. package/dist/tools/handlers/animation-handlers.d.ts.map +1 -0
  100. package/dist/tools/handlers/animation-handlers.js.map +1 -0
  101. package/dist/tools/handlers/argument-helper.d.ts.map +1 -0
  102. package/dist/tools/handlers/argument-helper.js.map +1 -0
  103. package/dist/tools/handlers/asset-handlers.d.ts.map +1 -0
  104. package/dist/tools/handlers/asset-handlers.js +5 -1
  105. package/dist/tools/handlers/asset-handlers.js.map +1 -0
  106. package/dist/tools/handlers/audio-handlers.d.ts.map +1 -0
  107. package/dist/tools/handlers/audio-handlers.js.map +1 -0
  108. package/dist/tools/handlers/blueprint-handlers.d.ts.map +1 -0
  109. package/dist/tools/handlers/blueprint-handlers.js +2 -1
  110. package/dist/tools/handlers/blueprint-handlers.js.map +1 -0
  111. package/dist/tools/handlers/common-handlers.d.ts.map +1 -0
  112. package/dist/tools/handlers/common-handlers.js.map +1 -0
  113. package/dist/tools/handlers/editor-handlers.d.ts.map +1 -0
  114. package/dist/tools/handlers/editor-handlers.js +12 -2
  115. package/dist/tools/handlers/editor-handlers.js.map +1 -0
  116. package/dist/tools/handlers/effect-handlers.d.ts.map +1 -0
  117. package/dist/tools/handlers/effect-handlers.js.map +1 -0
  118. package/dist/tools/handlers/environment-handlers.d.ts.map +1 -0
  119. package/dist/tools/handlers/environment-handlers.js.map +1 -0
  120. package/dist/tools/handlers/graph-handlers.d.ts.map +1 -0
  121. package/dist/tools/handlers/graph-handlers.js +61 -1
  122. package/dist/tools/handlers/graph-handlers.js.map +1 -0
  123. package/dist/tools/handlers/input-handlers.d.ts.map +1 -0
  124. package/dist/tools/handlers/input-handlers.js.map +1 -0
  125. package/dist/tools/handlers/inspect-handlers.d.ts.map +1 -0
  126. package/dist/tools/handlers/inspect-handlers.js.map +1 -0
  127. package/dist/tools/handlers/level-handlers.d.ts.map +1 -0
  128. package/dist/tools/handlers/level-handlers.js.map +1 -0
  129. package/dist/tools/handlers/lighting-handlers.d.ts.map +1 -0
  130. package/dist/tools/handlers/lighting-handlers.js +23 -1
  131. package/dist/tools/handlers/lighting-handlers.js.map +1 -0
  132. package/dist/tools/handlers/performance-handlers.d.ts.map +1 -0
  133. package/dist/tools/handlers/performance-handlers.js +15 -2
  134. package/dist/tools/handlers/performance-handlers.js.map +1 -0
  135. package/dist/tools/handlers/pipeline-handlers.d.ts.map +1 -0
  136. package/dist/tools/handlers/pipeline-handlers.js.map +1 -0
  137. package/dist/tools/handlers/sequence-handlers.d.ts.map +1 -0
  138. package/dist/tools/handlers/sequence-handlers.js.map +1 -0
  139. package/dist/tools/handlers/system-handlers.d.ts.map +1 -0
  140. package/dist/tools/handlers/system-handlers.js +16 -1
  141. package/dist/tools/handlers/system-handlers.js.map +1 -0
  142. package/dist/tools/input.d.ts.map +1 -0
  143. package/dist/tools/input.js +3 -1
  144. package/dist/tools/input.js.map +1 -0
  145. package/dist/tools/introspection.d.ts.map +1 -0
  146. package/dist/tools/introspection.js.map +1 -0
  147. package/dist/tools/landscape.d.ts.map +1 -0
  148. package/dist/tools/landscape.js +3 -1
  149. package/dist/tools/landscape.js.map +1 -0
  150. package/dist/tools/level.d.ts.map +1 -0
  151. package/dist/tools/level.js.map +1 -0
  152. package/dist/tools/lighting.d.ts.map +1 -0
  153. package/dist/tools/lighting.js +3 -1
  154. package/dist/tools/lighting.js.map +1 -0
  155. package/dist/tools/logs.d.ts.map +1 -0
  156. package/dist/tools/logs.js.map +1 -0
  157. package/dist/tools/materials.d.ts.map +1 -0
  158. package/dist/tools/materials.js +3 -1
  159. package/dist/tools/materials.js.map +1 -0
  160. package/dist/tools/niagara.d.ts.map +1 -0
  161. package/dist/tools/niagara.js +7 -5
  162. package/dist/tools/niagara.js.map +1 -0
  163. package/dist/tools/performance.d.ts.map +1 -0
  164. package/dist/tools/performance.js.map +1 -0
  165. package/dist/tools/physics.d.ts.map +1 -0
  166. package/dist/tools/physics.js +9 -7
  167. package/dist/tools/physics.js.map +1 -0
  168. package/dist/tools/property-dictionary.d.ts.map +1 -0
  169. package/dist/tools/property-dictionary.js.map +1 -0
  170. package/dist/tools/sequence.d.ts.map +1 -0
  171. package/dist/tools/sequence.js +3 -1
  172. package/dist/tools/sequence.js.map +1 -0
  173. package/dist/tools/tool-definition-utils.d.ts.map +1 -0
  174. package/dist/tools/tool-definition-utils.js.map +1 -0
  175. package/dist/tools/ui.d.ts.map +1 -0
  176. package/dist/tools/ui.js +3 -1
  177. package/dist/tools/ui.js.map +1 -0
  178. package/dist/types/automation-responses.d.ts.map +1 -0
  179. package/dist/types/automation-responses.js.map +1 -0
  180. package/dist/types/env.d.ts.map +1 -0
  181. package/dist/types/env.js.map +1 -0
  182. package/dist/types/handler-types.d.ts.map +1 -0
  183. package/dist/types/handler-types.js.map +1 -0
  184. package/dist/types/tool-interfaces.d.ts.map +1 -0
  185. package/dist/types/tool-interfaces.js.map +1 -0
  186. package/dist/types/tool-types.d.ts.map +1 -0
  187. package/dist/types/tool-types.js.map +1 -0
  188. package/dist/unreal-bridge.d.ts +1 -0
  189. package/dist/unreal-bridge.d.ts.map +1 -0
  190. package/dist/unreal-bridge.js +8 -0
  191. package/dist/unreal-bridge.js.map +1 -0
  192. package/dist/utils/command-validator.d.ts.map +1 -0
  193. package/dist/utils/command-validator.js.map +1 -0
  194. package/dist/utils/elicitation.d.ts.map +1 -0
  195. package/dist/utils/elicitation.js.map +1 -0
  196. package/dist/utils/error-handler.d.ts.map +1 -0
  197. package/dist/utils/error-handler.js.map +1 -0
  198. package/dist/utils/ini-reader.d.ts.map +1 -0
  199. package/dist/utils/ini-reader.js.map +1 -0
  200. package/dist/utils/logger.d.ts.map +1 -0
  201. package/dist/utils/logger.js.map +1 -0
  202. package/dist/utils/normalize.d.ts.map +1 -0
  203. package/dist/utils/normalize.js.map +1 -0
  204. package/dist/utils/path-security.d.ts.map +1 -0
  205. package/dist/utils/path-security.js.map +1 -0
  206. package/dist/utils/response-factory.d.ts.map +1 -0
  207. package/dist/utils/response-factory.js +3 -1
  208. package/dist/utils/response-factory.js.map +1 -0
  209. package/dist/utils/response-validator.d.ts.map +1 -0
  210. package/dist/utils/response-validator.js.map +1 -0
  211. package/dist/utils/result-helpers.d.ts.map +1 -0
  212. package/dist/utils/result-helpers.js.map +1 -0
  213. package/dist/utils/safe-json.d.ts.map +1 -0
  214. package/dist/utils/safe-json.js.map +1 -0
  215. package/dist/utils/unreal-command-queue.d.ts.map +1 -0
  216. package/dist/utils/unreal-command-queue.js.map +1 -0
  217. package/dist/utils/validation.d.ts.map +1 -0
  218. package/dist/utils/validation.js.map +1 -0
  219. package/dist/wasm/index.d.ts.map +1 -0
  220. package/dist/wasm/index.js.map +1 -0
  221. package/package.json +12 -34
  222. package/server.json +2 -2
  223. package/.dockerignore +0 -57
  224. package/.env.example +0 -26
  225. package/.env.production +0 -61
  226. package/.eslintrc.json +0 -0
  227. package/.eslintrc.override.json +0 -8
  228. package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -94
  229. package/.github/ISSUE_TEMPLATE/config.yml +0 -8
  230. package/.github/ISSUE_TEMPLATE/feature_request.yml +0 -56
  231. package/.github/copilot-instructions.md +0 -478
  232. package/.github/dependabot.yml +0 -19
  233. package/.github/labeler.yml +0 -24
  234. package/.github/labels.yml +0 -70
  235. package/.github/pull_request_template.md +0 -42
  236. package/.github/release-drafter-config.yml +0 -51
  237. package/.github/workflows/auto-merge.yml +0 -38
  238. package/.github/workflows/ci.yml +0 -38
  239. package/.github/workflows/dependency-review.yml +0 -17
  240. package/.github/workflows/gemini-issue-triage.yml +0 -172
  241. package/.github/workflows/greetings.yml +0 -27
  242. package/.github/workflows/labeler.yml +0 -17
  243. package/.github/workflows/links.yml +0 -80
  244. package/.github/workflows/pr-size-labeler.yml +0 -137
  245. package/.github/workflows/publish-mcp.yml +0 -79
  246. package/.github/workflows/release-drafter.yml +0 -24
  247. package/.github/workflows/release.yml +0 -112
  248. package/.github/workflows/semantic-pull-request.yml +0 -35
  249. package/.github/workflows/smoke-test.yml +0 -36
  250. package/.github/workflows/stale.yml +0 -28
  251. package/CONTRIBUTING.md +0 -140
  252. package/Dockerfile +0 -37
  253. package/GEMINI.md +0 -115
  254. package/Public/Plugin_setup_guide.mp4 +0 -0
  255. package/Public/icon.png +0 -0
  256. package/claude_desktop_config_example.json +0 -15
  257. package/dist/types/responses.d.ts +0 -249
  258. package/dist/types/responses.js +0 -2
  259. package/docs/GraphQL-API.md +0 -888
  260. package/docs/Migration-Guide-v0.5.0.md +0 -684
  261. package/docs/Roadmap.md +0 -53
  262. package/docs/WebAssembly-Integration.md +0 -628
  263. package/docs/editor-plugin-extension.md +0 -370
  264. package/docs/handler-mapping.md +0 -249
  265. package/docs/native-automation-progress.md +0 -128
  266. package/docs/testing-guide.md +0 -423
  267. package/eslint.config.mjs +0 -68
  268. package/mcp-config-example.json +0 -14
  269. package/plugins/McpAutomationBridge/Config/FilterPlugin.ini +0 -8
  270. package/plugins/McpAutomationBridge/McpAutomationBridge.uplugin +0 -64
  271. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/McpAutomationBridge.Build.cs +0 -189
  272. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridgeGlobals.cpp +0 -22
  273. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridgeGlobals.h +0 -30
  274. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridgeHelpers.h +0 -1983
  275. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridgeModule.cpp +0 -72
  276. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridgeSettings.cpp +0 -46
  277. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridgeSubsystem.cpp +0 -846
  278. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_AnimationHandlers.cpp +0 -2393
  279. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_AssetQueryHandlers.cpp +0 -300
  280. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_AssetWorkflowHandlers.cpp +0 -2807
  281. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_AudioHandlers.cpp +0 -1087
  282. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_BehaviorTreeHandlers.cpp +0 -488
  283. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_BlueprintCreationHandlers.cpp +0 -643
  284. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_BlueprintCreationHandlers.h +0 -31
  285. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_BlueprintGraphHandlers.cpp +0 -1094
  286. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_BlueprintHandlers.cpp +0 -5750
  287. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_BlueprintHandlers_List.cpp +0 -152
  288. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_ControlHandlers.cpp +0 -2614
  289. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_DebugHandlers.cpp +0 -42
  290. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_EditorFunctionHandlers.cpp +0 -1237
  291. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_EffectHandlers.cpp +0 -1725
  292. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_EnvironmentHandlers.cpp +0 -2265
  293. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_FoliageHandlers.cpp +0 -954
  294. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_InputHandlers.cpp +0 -209
  295. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_InsightsHandlers.cpp +0 -41
  296. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_LandscapeHandlers.cpp +0 -1164
  297. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_LevelHandlers.cpp +0 -762
  298. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_LightingHandlers.cpp +0 -663
  299. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_LogHandlers.cpp +0 -136
  300. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_MaterialGraphHandlers.cpp +0 -494
  301. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_NiagaraGraphHandlers.cpp +0 -278
  302. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_NiagaraHandlers.cpp +0 -625
  303. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_PerformanceHandlers.cpp +0 -401
  304. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_PipelineHandlers.cpp +0 -67
  305. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_ProcessRequest.cpp +0 -472
  306. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_PropertyHandlers.cpp +0 -2634
  307. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_RenderHandlers.cpp +0 -189
  308. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_SCSHandlers.cpp +0 -917
  309. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_SCSHandlers.h +0 -39
  310. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_SequenceHandlers.cpp +0 -2706
  311. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_SequencerHandlers.cpp +0 -519
  312. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_TestHandlers.cpp +0 -38
  313. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_UiHandlers.cpp +0 -668
  314. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpAutomationBridge_WorldPartitionHandlers.cpp +0 -346
  315. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpBridgeWebSocket.cpp +0 -1345
  316. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpBridgeWebSocket.h +0 -149
  317. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Private/McpConnectionManager.cpp +0 -782
  318. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Public/McpAutomationBridgeSettings.h +0 -115
  319. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Public/McpAutomationBridgeSubsystem.h +0 -796
  320. package/plugins/McpAutomationBridge/Source/McpAutomationBridge/Public/McpConnectionManager.h +0 -117
  321. package/scripts/check-unreal-connection.mjs +0 -19
  322. package/scripts/clean-tmp.js +0 -23
  323. package/scripts/patch-wasm.js +0 -26
  324. package/scripts/run-all-tests.mjs +0 -136
  325. package/scripts/smoke-test.ts +0 -94
  326. package/scripts/sync-mcp-plugin.js +0 -143
  327. package/scripts/test-no-plugin-alternates.mjs +0 -113
  328. package/scripts/validate-server.js +0 -46
  329. package/scripts/verify-automation-bridge.js +0 -200
  330. package/src/automation/bridge.ts +0 -630
  331. package/src/automation/connection-manager.ts +0 -148
  332. package/src/automation/handshake.ts +0 -99
  333. package/src/automation/index.ts +0 -2
  334. package/src/automation/message-handler.ts +0 -192
  335. package/src/automation/request-tracker.ts +0 -155
  336. package/src/automation/types.ts +0 -108
  337. package/src/cli.ts +0 -34
  338. package/src/config/class-aliases.ts +0 -65
  339. package/src/config.ts +0 -73
  340. package/src/constants.ts +0 -29
  341. package/src/graphql/loaders.ts +0 -244
  342. package/src/graphql/resolvers.ts +0 -1008
  343. package/src/graphql/schema.ts +0 -452
  344. package/src/graphql/server.ts +0 -156
  345. package/src/graphql/types.ts +0 -10
  346. package/src/handlers/resource-handlers.ts +0 -186
  347. package/src/index.ts +0 -243
  348. package/src/resources/actors.ts +0 -127
  349. package/src/resources/assets.ts +0 -286
  350. package/src/resources/levels.ts +0 -68
  351. package/src/server/resource-registry.ts +0 -47
  352. package/src/server/tool-registry.ts +0 -354
  353. package/src/server-setup.ts +0 -114
  354. package/src/services/health-monitor.ts +0 -132
  355. package/src/services/metrics-server.ts +0 -176
  356. package/src/tools/actors.ts +0 -564
  357. package/src/tools/animation.ts +0 -941
  358. package/src/tools/assets.ts +0 -394
  359. package/src/tools/audio.ts +0 -499
  360. package/src/tools/base-tool.ts +0 -52
  361. package/src/tools/behavior-tree.ts +0 -45
  362. package/src/tools/blueprint.ts +0 -940
  363. package/src/tools/consolidated-tool-definitions.ts +0 -1256
  364. package/src/tools/consolidated-tool-handlers.ts +0 -302
  365. package/src/tools/debug.ts +0 -622
  366. package/src/tools/dynamic-handler-registry.ts +0 -33
  367. package/src/tools/editor.ts +0 -435
  368. package/src/tools/engine.ts +0 -43
  369. package/src/tools/environment.ts +0 -281
  370. package/src/tools/foliage.ts +0 -596
  371. package/src/tools/handlers/actor-handlers.ts +0 -244
  372. package/src/tools/handlers/animation-handlers.ts +0 -237
  373. package/src/tools/handlers/argument-helper.ts +0 -142
  374. package/src/tools/handlers/asset-handlers.ts +0 -550
  375. package/src/tools/handlers/audio-handlers.ts +0 -194
  376. package/src/tools/handlers/blueprint-handlers.ts +0 -380
  377. package/src/tools/handlers/common-handlers.ts +0 -108
  378. package/src/tools/handlers/editor-handlers.ts +0 -124
  379. package/src/tools/handlers/effect-handlers.ts +0 -224
  380. package/src/tools/handlers/environment-handlers.ts +0 -183
  381. package/src/tools/handlers/graph-handlers.ts +0 -117
  382. package/src/tools/handlers/input-handlers.ts +0 -28
  383. package/src/tools/handlers/inspect-handlers.ts +0 -450
  384. package/src/tools/handlers/level-handlers.ts +0 -253
  385. package/src/tools/handlers/lighting-handlers.ts +0 -151
  386. package/src/tools/handlers/performance-handlers.ts +0 -132
  387. package/src/tools/handlers/pipeline-handlers.ts +0 -194
  388. package/src/tools/handlers/sequence-handlers.ts +0 -438
  389. package/src/tools/handlers/system-handlers.ts +0 -564
  390. package/src/tools/input.ts +0 -160
  391. package/src/tools/introspection.ts +0 -689
  392. package/src/tools/landscape.ts +0 -649
  393. package/src/tools/level.ts +0 -989
  394. package/src/tools/lighting.ts +0 -1052
  395. package/src/tools/logs.ts +0 -219
  396. package/src/tools/materials.ts +0 -295
  397. package/src/tools/niagara.ts +0 -485
  398. package/src/tools/performance.ts +0 -661
  399. package/src/tools/physics.ts +0 -679
  400. package/src/tools/property-dictionary.ts +0 -98
  401. package/src/tools/sequence.ts +0 -385
  402. package/src/tools/tool-definition-utils.ts +0 -35
  403. package/src/tools/ui.ts +0 -452
  404. package/src/types/automation-responses.ts +0 -119
  405. package/src/types/env.ts +0 -17
  406. package/src/types/handler-types.ts +0 -442
  407. package/src/types/responses.ts +0 -355
  408. package/src/types/tool-interfaces.ts +0 -250
  409. package/src/types/tool-types.ts +0 -575
  410. package/src/unreal-bridge.ts +0 -693
  411. package/src/utils/command-validator.ts +0 -139
  412. package/src/utils/elicitation.ts +0 -132
  413. package/src/utils/error-handler.ts +0 -287
  414. package/src/utils/ini-reader.ts +0 -86
  415. package/src/utils/logger.ts +0 -35
  416. package/src/utils/normalize.test.ts +0 -162
  417. package/src/utils/normalize.ts +0 -146
  418. package/src/utils/path-security.ts +0 -43
  419. package/src/utils/response-factory.ts +0 -44
  420. package/src/utils/response-validator.ts +0 -395
  421. package/src/utils/result-helpers.ts +0 -195
  422. package/src/utils/safe-json.test.ts +0 -90
  423. package/src/utils/safe-json.ts +0 -70
  424. package/src/utils/unreal-command-queue.ts +0 -166
  425. package/src/utils/validation.test.ts +0 -184
  426. package/src/utils/validation.ts +0 -312
  427. package/src/wasm/index.ts +0 -838
  428. package/test-server.mjs +0 -100
  429. package/tests/test-animation.mjs +0 -369
  430. package/tests/test-asset-advanced.mjs +0 -82
  431. package/tests/test-asset-graph.mjs +0 -311
  432. package/tests/test-audio.mjs +0 -417
  433. package/tests/test-automation-timeouts.mjs +0 -98
  434. package/tests/test-behavior-tree.mjs +0 -444
  435. package/tests/test-blueprint-graph.mjs +0 -410
  436. package/tests/test-blueprint.mjs +0 -577
  437. package/tests/test-client-mode.mjs +0 -86
  438. package/tests/test-console-command.mjs +0 -56
  439. package/tests/test-control-actor.mjs +0 -425
  440. package/tests/test-control-editor.mjs +0 -112
  441. package/tests/test-graphql.mjs +0 -372
  442. package/tests/test-input.mjs +0 -349
  443. package/tests/test-inspect.mjs +0 -302
  444. package/tests/test-landscape.mjs +0 -316
  445. package/tests/test-lighting.mjs +0 -428
  446. package/tests/test-manage-asset.mjs +0 -438
  447. package/tests/test-manage-level.mjs +0 -89
  448. package/tests/test-materials.mjs +0 -356
  449. package/tests/test-niagara.mjs +0 -185
  450. package/tests/test-no-inline-python.mjs +0 -122
  451. package/tests/test-performance.mjs +0 -539
  452. package/tests/test-plugin-handshake.mjs +0 -82
  453. package/tests/test-runner.mjs +0 -993
  454. package/tests/test-sequence.mjs +0 -104
  455. package/tests/test-system.mjs +0 -96
  456. package/tests/test-wasm.mjs +0 -283
  457. package/tests/test-world-partition.mjs +0 -215
  458. package/tsconfig.json +0 -56
  459. package/vitest.config.ts +0 -35
  460. package/wasm/Cargo.lock +0 -363
  461. package/wasm/Cargo.toml +0 -42
  462. package/wasm/LICENSE +0 -21
  463. package/wasm/README.md +0 -253
  464. package/wasm/src/dependency_resolver.rs +0 -377
  465. package/wasm/src/lib.rs +0 -153
  466. package/wasm/src/property_parser.rs +0 -271
  467. package/wasm/src/transform_math.rs +0 -396
  468. package/wasm/tests/integration.rs +0 -109
@@ -1,1345 +0,0 @@
1
- #include "McpBridgeWebSocket.h"
2
- #include "McpAutomationBridgeSubsystem.h"
3
-
4
- #include "Async/Async.h"
5
- #include "Containers/StringConv.h"
6
- #include "HAL/Event.h"
7
- #include "HAL/PlatformAtomics.h"
8
- #include "HAL/PlatformProcess.h"
9
- #include "HAL/RunnableThread.h"
10
- #include "IPAddress.h"
11
- #include "Logging/LogMacros.h"
12
- #include "Math/UnrealMathUtility.h"
13
- #include "Misc/Base64.h"
14
- #include "Misc/ScopeLock.h"
15
- #include "Misc/SecureHash.h"
16
- #include "Misc/StringBuilder.h"
17
- #include "Misc/Timespan.h"
18
- #include "SocketSubsystem.h"
19
- #include "Sockets.h"
20
- #include "String/LexFromString.h"
21
-
22
-
23
- namespace {
24
- constexpr const TCHAR *WebSocketGuid =
25
- TEXT("258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
26
- constexpr uint8 OpCodeContinuation = 0x0;
27
- constexpr uint8 OpCodeText = 0x1;
28
- constexpr uint8 OpCodeBinary = 0x2;
29
- constexpr uint8 OpCodeClose = 0x8;
30
- constexpr uint8 OpCodePing = 0x9;
31
- constexpr uint8 OpCodePong = 0xA;
32
-
33
- struct FParsedWebSocketUrl {
34
- FString Host;
35
- int32 Port = 80;
36
- FString PathWithQuery;
37
- };
38
-
39
- bool ParseWebSocketUrl(const FString &InUrl, FParsedWebSocketUrl &OutParsed,
40
- FString &OutError) {
41
- const FString Trimmed = InUrl.TrimStartAndEnd();
42
- if (Trimmed.IsEmpty()) {
43
- OutError = TEXT("WebSocket URL is empty.");
44
- return false;
45
- }
46
-
47
- static const FString SchemePrefix(TEXT("ws://"));
48
- if (!Trimmed.StartsWith(SchemePrefix, ESearchCase::IgnoreCase)) {
49
- OutError = TEXT("Only ws:// scheme is supported.");
50
- return false;
51
- }
52
-
53
- const FString Remainder = Trimmed.Mid(SchemePrefix.Len());
54
- FString HostPort;
55
- FString PathRemainder;
56
- if (!Remainder.Split(TEXT("/"), &HostPort, &PathRemainder,
57
- ESearchCase::CaseSensitive, ESearchDir::FromStart)) {
58
- HostPort = Remainder;
59
- PathRemainder.Reset();
60
- }
61
-
62
- HostPort = HostPort.TrimStartAndEnd();
63
- if (HostPort.IsEmpty()) {
64
- OutError = TEXT("WebSocket URL missing host.");
65
- return false;
66
- }
67
-
68
- FString Host;
69
- int32 Port = 80;
70
-
71
- if (HostPort.StartsWith(TEXT("["))) {
72
- int32 ClosingBracketIndex = INDEX_NONE;
73
- if (!HostPort.FindChar(TEXT(']'), ClosingBracketIndex)) {
74
- OutError = TEXT("Invalid IPv6 WebSocket host.");
75
- return false;
76
- }
77
-
78
- Host = HostPort.Mid(1, ClosingBracketIndex - 1);
79
- const int32 PortSeparatorIndex = HostPort.Find(
80
- TEXT(":"), ESearchCase::CaseSensitive, ESearchDir::FromEnd);
81
- if (PortSeparatorIndex > ClosingBracketIndex) {
82
- const FString PortString = HostPort.Mid(PortSeparatorIndex + 1);
83
- if (!PortString.IsEmpty() && !LexTryParseString(Port, *PortString)) {
84
- OutError = TEXT("Invalid WebSocket port.");
85
- return false;
86
- }
87
- }
88
- } else {
89
- FString PortString;
90
- if (HostPort.Split(TEXT(":"), &Host, &PortString,
91
- ESearchCase::CaseSensitive, ESearchDir::FromEnd)) {
92
- Host = Host.TrimStartAndEnd();
93
- PortString = PortString.TrimStartAndEnd();
94
-
95
- if (!PortString.IsEmpty()) {
96
- if (!LexTryParseString(Port, *PortString)) {
97
- OutError = TEXT("Invalid WebSocket port.");
98
- return false;
99
- }
100
- }
101
- } else {
102
- Host = HostPort;
103
- }
104
- }
105
-
106
- Host = Host.TrimStartAndEnd();
107
- if (Host.IsEmpty()) {
108
- OutError = TEXT("WebSocket URL missing host.");
109
- return false;
110
- }
111
-
112
- if (Port <= 0) {
113
- OutError = TEXT("WebSocket port must be positive.");
114
- return false;
115
- }
116
-
117
- FString PathWithQuery;
118
- if (PathRemainder.IsEmpty()) {
119
- PathWithQuery = TEXT("/");
120
- } else {
121
- PathWithQuery = TEXT("/") + PathRemainder;
122
- }
123
-
124
- OutParsed.Host = Host;
125
- OutParsed.Port = Port;
126
- OutParsed.PathWithQuery = PathWithQuery;
127
- return true;
128
- }
129
-
130
- uint16 ToNetwork16(uint16 Value) {
131
- #if PLATFORM_LITTLE_ENDIAN
132
- return static_cast<uint16>(((Value & 0x00FF) << 8) | ((Value & 0xFF00) >> 8));
133
- #else
134
- return Value;
135
- #endif
136
- }
137
-
138
- uint16 FromNetwork16(uint16 Value) { return ToNetwork16(Value); }
139
-
140
- uint64 ToNetwork64(uint64 Value) {
141
- #if PLATFORM_LITTLE_ENDIAN
142
- return ((Value & 0x00000000000000FFULL) << 56) |
143
- ((Value & 0x000000000000FF00ULL) << 40) |
144
- ((Value & 0x0000000000FF0000ULL) << 24) |
145
- ((Value & 0x00000000FF000000ULL) << 8) |
146
- ((Value & 0x000000FF00000000ULL) >> 8) |
147
- ((Value & 0x0000FF0000000000ULL) >> 24) |
148
- ((Value & 0x00FF000000000000ULL) >> 40) |
149
- ((Value & 0xFF00000000000000ULL) >> 56);
150
- #else
151
- return Value;
152
- #endif
153
- }
154
-
155
- uint64 FromNetwork64(uint64 Value) { return ToNetwork64(Value); }
156
-
157
- FString BytesToStringView(const TArray<uint8> &Data) {
158
- if (Data.Num() == 0) {
159
- return FString();
160
- }
161
- // Convert UTF-8 bytes to TCHAR using an explicit length-aware converter
162
- // to avoid reading beyond the provided buffer. The previous implementation
163
- // used a null-terminated style conversion which could read past the
164
- // payload and include stray bytes from subsequent socket reads, causing
165
- // JSON parse failures on the receiving side.
166
- const ANSICHAR *Utf8Ptr = reinterpret_cast<const ANSICHAR *>(Data.GetData());
167
- FUTF8ToTCHAR Converter(Utf8Ptr, Data.Num());
168
- if (Converter.Length() <= 0) {
169
- return FString();
170
- }
171
- return FString(Converter.Length(), Converter.Get());
172
- }
173
-
174
- void DispatchOnGameThread(TFunction<void()> &&Fn) {
175
- if (IsInGameThread()) {
176
- Fn();
177
- return;
178
- }
179
-
180
- AsyncTask(ENamedThreads::GameThread, MoveTemp(Fn));
181
- }
182
-
183
- FString DescribeSocketError(ISocketSubsystem *SocketSubsystem,
184
- const TCHAR *Context) {
185
- if (!SocketSubsystem) {
186
- return FString::Printf(TEXT("%s (no socket subsystem)"), Context);
187
- }
188
-
189
- const ESocketErrors LastErrorCode = SocketSubsystem->GetLastErrorCode();
190
- const FString Description = SocketSubsystem->GetSocketError(LastErrorCode);
191
- return FString::Printf(TEXT("%s (error=%d, %s)"), Context,
192
- static_cast<int32>(LastErrorCode), *Description);
193
- }
194
- } // namespace
195
-
196
- FMcpBridgeWebSocket::FMcpBridgeWebSocket(
197
- const FString &InUrl, const FString &InProtocols,
198
- const TMap<FString, FString> &InHeaders)
199
- : Url(InUrl), Socket(nullptr), Port(0), Protocols(InProtocols),
200
- Headers(InHeaders), ListenHost(), PendingReceived(),
201
- FragmentAccumulator(), bFragmentMessageActive(false), SelfWeakPtr(),
202
- bServerMode(false), bServerAcceptedConnection(false),
203
- ListenSocket(nullptr), Thread(nullptr), StopEvent(nullptr),
204
- ClientSockets(), ListenBacklog(10), AcceptSleepSeconds(0.01f),
205
- bConnected(false), bListening(false), bStopping(false) {
206
- HandlerReadyEvent = nullptr;
207
- bHandlerRegistered = false;
208
- }
209
-
210
- FMcpBridgeWebSocket::FMcpBridgeWebSocket(int32 InPort, const FString &InHost,
211
- int32 InListenBacklog,
212
- float InAcceptSleepSeconds)
213
- : Url(), Socket(nullptr), Port(InPort), Protocols(TEXT("mcp-automation")),
214
- Headers(), ListenHost(InHost), PendingReceived(), FragmentAccumulator(),
215
- bFragmentMessageActive(false), SelfWeakPtr(), bServerMode(true),
216
- bServerAcceptedConnection(false), ListenSocket(nullptr), Thread(nullptr),
217
- StopEvent(nullptr), ClientSockets(), ListenBacklog(InListenBacklog),
218
- AcceptSleepSeconds(InAcceptSleepSeconds), bConnected(false),
219
- bListening(false), bStopping(false) {
220
- HandlerReadyEvent = nullptr;
221
- bHandlerRegistered = false;
222
- }
223
-
224
- FMcpBridgeWebSocket::FMcpBridgeWebSocket(FSocket *InClientSocket)
225
- : Url(), Socket(InClientSocket), Port(0), Protocols(TEXT("mcp-automation")),
226
- Headers(), ListenHost(), PendingReceived(), FragmentAccumulator(),
227
- bFragmentMessageActive(false), SelfWeakPtr(), bServerMode(false),
228
- bServerAcceptedConnection(true), ListenSocket(nullptr), Thread(nullptr),
229
- StopEvent(nullptr), ClientSockets(), ListenBacklog(10),
230
- AcceptSleepSeconds(0.01f), bConnected(true), bListening(false),
231
- bStopping(false) {
232
- HandlerReadyEvent = nullptr;
233
- bHandlerRegistered = false;
234
- }
235
-
236
- FMcpBridgeWebSocket::~FMcpBridgeWebSocket() {
237
- Close();
238
- if (HandlerReadyEvent) {
239
- FPlatformProcess::ReturnSynchEventToPool(HandlerReadyEvent);
240
- HandlerReadyEvent = nullptr;
241
- }
242
- if (Thread) {
243
- Thread->WaitForCompletion();
244
- delete Thread;
245
- Thread = nullptr;
246
- }
247
- if (StopEvent) {
248
- FPlatformProcess::ReturnSynchEventToPool(StopEvent);
249
- StopEvent = nullptr;
250
- }
251
- if (FSocket *LocalSocket = DetachSocket()) {
252
- LocalSocket->Close();
253
- ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(LocalSocket);
254
- }
255
- }
256
-
257
- FSocket *FMcpBridgeWebSocket::DetachSocket() {
258
- return static_cast<FSocket *>(FPlatformAtomics::InterlockedExchangePtr(
259
- reinterpret_cast<void **>(&Socket), nullptr));
260
- }
261
-
262
- void FMcpBridgeWebSocket::NotifyMessageHandlerRegistered() {
263
- bHandlerRegistered = true;
264
- if (HandlerReadyEvent) {
265
- HandlerReadyEvent->Trigger();
266
- }
267
- }
268
-
269
- void FMcpBridgeWebSocket::InitializeWeakSelf(
270
- const TSharedPtr<FMcpBridgeWebSocket> &InShared) {
271
- SelfWeakPtr = InShared;
272
- }
273
-
274
- void FMcpBridgeWebSocket::Connect() {
275
- if (Thread) {
276
- return;
277
- }
278
-
279
- bStopping = false;
280
- StopEvent = FPlatformProcess::GetSynchEventFromPool(true);
281
- Thread = FRunnableThread::Create(this, TEXT("FMcpBridgeWebSocketWorker"), 0,
282
- TPri_Normal);
283
- if (!Thread) {
284
- DispatchOnGameThread([WeakThis = SelfWeakPtr] {
285
- if (TSharedPtr<FMcpBridgeWebSocket> Pinned = WeakThis.Pin()) {
286
- Pinned->ConnectionErrorDelegate.Broadcast(
287
- TEXT("Failed to create WebSocket worker thread."));
288
- }
289
- });
290
- }
291
- }
292
-
293
- void FMcpBridgeWebSocket::Listen() {
294
- if (Thread || !bServerMode) {
295
- return;
296
- }
297
-
298
- bStopping = false;
299
- StopEvent = FPlatformProcess::GetSynchEventFromPool(true);
300
- UE_LOG(LogMcpAutomationBridgeSubsystem, Log,
301
- TEXT("Spawning MCP automation server thread for %s:%d"), *ListenHost,
302
- Port);
303
- Thread = FRunnableThread::Create(
304
- this, TEXT("FMcpBridgeWebSocketServerWorker"), 0, TPri_Normal);
305
- if (!Thread) {
306
- UE_LOG(LogMcpAutomationBridgeSubsystem, Error,
307
- TEXT("Failed to create server thread for MCP automation bridge."));
308
- DispatchOnGameThread([WeakThis = SelfWeakPtr] {
309
- if (TSharedPtr<FMcpBridgeWebSocket> Pinned = WeakThis.Pin()) {
310
- Pinned->ConnectionErrorDelegate.Broadcast(
311
- TEXT("Failed to create WebSocket server worker thread."));
312
- }
313
- });
314
- }
315
- }
316
-
317
- void FMcpBridgeWebSocket::Close(int32 StatusCode, const FString &Reason) {
318
- bStopping = true;
319
- if (StopEvent) {
320
- StopEvent->Trigger();
321
- }
322
-
323
- if (FSocket *LocalSocket = DetachSocket()) {
324
- LocalSocket->Close();
325
- ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(LocalSocket);
326
- }
327
- }
328
-
329
- bool FMcpBridgeWebSocket::Send(const FString &Data) {
330
- FTCHARToUTF8 Converter(*Data);
331
- return Send(Converter.Get(), Converter.Length());
332
- }
333
-
334
- bool FMcpBridgeWebSocket::Send(const void *Data, SIZE_T Length) {
335
- if (!IsConnected() || !Socket) {
336
- return false;
337
- }
338
-
339
- return SendTextFrame(Data, Length);
340
- }
341
-
342
- bool FMcpBridgeWebSocket::IsConnected() const { return bConnected; }
343
-
344
- bool FMcpBridgeWebSocket::IsListening() const { return bListening; }
345
-
346
- void FMcpBridgeWebSocket::SendHeartbeatPing() {
347
- SendControlFrame(OpCodePing, TArray<uint8>());
348
- }
349
-
350
- bool FMcpBridgeWebSocket::Init() { return true; }
351
-
352
- uint32 FMcpBridgeWebSocket::Run() {
353
- if (bServerMode) {
354
- return RunServer();
355
- } else {
356
- return RunClient();
357
- }
358
- }
359
-
360
- uint32 FMcpBridgeWebSocket::RunClient() {
361
- if (bServerAcceptedConnection) {
362
- if (!PerformServerHandshake()) {
363
- return 0;
364
- }
365
- } else {
366
- if (!PerformHandshake()) {
367
- return 0;
368
- }
369
- }
370
-
371
- bConnected = true;
372
- UE_LOG(
373
- LogMcpAutomationBridgeSubsystem, Log,
374
- TEXT("FMcpBridgeWebSocket connection established (serverAccepted=%s)."),
375
- bServerAcceptedConnection ? TEXT("true") : TEXT("false"));
376
- DispatchOnGameThread([WeakThis = SelfWeakPtr] {
377
- if (TSharedPtr<FMcpBridgeWebSocket> Pinned = WeakThis.Pin()) {
378
- Pinned->ConnectedDelegate.Broadcast(Pinned);
379
- }
380
- });
381
-
382
- // If this connection was accepted by the server thread (i.e. a remote
383
- // client connected to the plugin), wait a short time for the game
384
- // thread to attach message handlers. The client is likely to send the
385
- // application-level 'bridge_hello' immediately after the upgrade; if
386
- // the game thread hasn't attached its OnMessage handler yet we risk
387
- // losing that first frame. Wait up to a moderate timeout for the
388
- // handler registration signal.
389
- if (bServerAcceptedConnection) {
390
- // Lazily create the event used to wait for the handler if it
391
- // hasn't been allocated yet. Use the event pool to avoid
392
- // continuously allocating objects.
393
- if (!HandlerReadyEvent) {
394
- HandlerReadyEvent = FPlatformProcess::GetSynchEventFromPool(true);
395
- }
396
-
397
- constexpr double MaxWaitSeconds = 0.5; // 500 ms
398
- UE_LOG(LogMcpAutomationBridgeSubsystem, Verbose,
399
- TEXT("Awaiting message handler registration for new client "
400
- "connection (max %.0f ms)."),
401
- MaxWaitSeconds * 1000.0);
402
- if (HandlerReadyEvent->Wait(FTimespan::FromSeconds(MaxWaitSeconds))) {
403
- // Event triggered by game thread
404
- }
405
- if (!bHandlerRegistered) {
406
- UE_LOG(LogMcpAutomationBridgeSubsystem, Verbose,
407
- TEXT("Message handler registration not observed in time; "
408
- "proceeding without explicit synchronization."));
409
- }
410
- }
411
-
412
- while (!bStopping) {
413
- if (!ReceiveFrame()) {
414
- break;
415
- }
416
- }
417
-
418
- TearDown(TEXT("Socket loop finished."), true, 1000);
419
- return 0;
420
- }
421
-
422
- uint32 FMcpBridgeWebSocket::RunServer() {
423
- ISocketSubsystem *SocketSubsystem =
424
- ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM);
425
- UE_LOG(LogMcpAutomationBridgeSubsystem, Log,
426
- TEXT("FMcpBridgeWebSocket::RunServer begin (host=%s, port=%d)"),
427
- *ListenHost, Port);
428
- ListenSocket = SocketSubsystem->CreateSocket(
429
- NAME_Stream, TEXT("McpAutomationBridgeListenSocket"), false);
430
- if (!ListenSocket) {
431
- const FString ErrorMessage = DescribeSocketError(
432
- SocketSubsystem, TEXT("Failed to create listen socket"));
433
- UE_LOG(LogMcpAutomationBridgeSubsystem, Error, TEXT("%s"), *ErrorMessage);
434
- DispatchOnGameThread([WeakThis = SelfWeakPtr, ErrorMessage] {
435
- if (TSharedPtr<FMcpBridgeWebSocket> Pinned = WeakThis.Pin()) {
436
- Pinned->ConnectionErrorDelegate.Broadcast(ErrorMessage);
437
- }
438
- });
439
- return 0;
440
- }
441
-
442
- ListenSocket->SetReuseAddr(true);
443
- ListenSocket->SetNonBlocking(false);
444
- UE_LOG(LogMcpAutomationBridgeSubsystem, Log, TEXT("Listen socket created."));
445
-
446
- TSharedRef<FInternetAddr> ListenAddr = SocketSubsystem->CreateInternetAddr();
447
-
448
- bool bResolvedHost = false;
449
- bool bExplicitBindAll = false;
450
-
451
- if (!ListenHost.IsEmpty()) {
452
- FString HostToBind = ListenHost;
453
- if (HostToBind.Equals(TEXT("localhost"), ESearchCase::IgnoreCase)) {
454
- HostToBind = TEXT("127.0.0.1");
455
- }
456
-
457
- bool bIsValidIp = false;
458
- ListenAddr->SetIp(*HostToBind, bIsValidIp);
459
- if (bIsValidIp) {
460
- bResolvedHost = true;
461
- }
462
-
463
- bExplicitBindAll = HostToBind.Equals(TEXT("0.0.0.0"), ESearchCase::IgnoreCase) ||
464
- HostToBind.Equals(TEXT("::"), ESearchCase::IgnoreCase);
465
- }
466
-
467
- if (!bResolvedHost) {
468
- if (!bExplicitBindAll) {
469
- UE_LOG(LogMcpAutomationBridgeSubsystem, Warning,
470
- TEXT("Invalid ListenHost '%s'. Falling back to 127.0.0.1 for safety. To bind all interfaces, explicitly set ListenHost=0.0.0.0."),
471
- *ListenHost);
472
-
473
- bool bFallbackIsValidIp = false;
474
- ListenAddr->SetIp(TEXT("127.0.0.1"), bFallbackIsValidIp);
475
- bResolvedHost = bFallbackIsValidIp;
476
- } else {
477
- ListenAddr->SetAnyAddress();
478
- }
479
- }
480
-
481
- ListenAddr->SetPort(Port);
482
-
483
- if (!ListenSocket->Bind(*ListenAddr)) {
484
- const FString ErrorMessage = DescribeSocketError(
485
- SocketSubsystem, TEXT("Failed to bind listen socket"));
486
- UE_LOG(LogMcpAutomationBridgeSubsystem, Error, TEXT("%s"), *ErrorMessage);
487
- DispatchOnGameThread([WeakThis = SelfWeakPtr, ErrorMessage] {
488
- if (TSharedPtr<FMcpBridgeWebSocket> Pinned = WeakThis.Pin()) {
489
- Pinned->ConnectionErrorDelegate.Broadcast(ErrorMessage);
490
- }
491
- });
492
- return 0;
493
- }
494
- UE_LOG(LogMcpAutomationBridgeSubsystem, Log,
495
- TEXT("Listen socket bound to %s."), *ListenAddr->ToString(false));
496
-
497
- if (!ListenSocket->Listen(ListenBacklog > 0 ? ListenBacklog : 10)) {
498
- const FString ErrorMessage = DescribeSocketError(
499
- SocketSubsystem, TEXT("Failed to listen on socket"));
500
- UE_LOG(LogMcpAutomationBridgeSubsystem, Error, TEXT("%s"), *ErrorMessage);
501
- DispatchOnGameThread([WeakThis = SelfWeakPtr, ErrorMessage] {
502
- if (TSharedPtr<FMcpBridgeWebSocket> Pinned = WeakThis.Pin()) {
503
- Pinned->ConnectionErrorDelegate.Broadcast(ErrorMessage);
504
- }
505
- });
506
- return 0;
507
- }
508
-
509
- bListening = true;
510
- const FString BoundAddress = ListenAddr->ToString(false);
511
- UE_LOG(LogMcpAutomationBridgeSubsystem, Log,
512
- TEXT("MCP Automation Bridge listening on %s"), *BoundAddress);
513
- DispatchOnGameThread([WeakThis = SelfWeakPtr, BoundAddress] {
514
- if (TSharedPtr<FMcpBridgeWebSocket> Pinned = WeakThis.Pin()) {
515
- Pinned->ConnectedDelegate.Broadcast(Pinned); // Server ready event
516
- }
517
- });
518
-
519
- while (!bStopping) {
520
- // Accept incoming connections
521
- FSocket *ClientSocket =
522
- ListenSocket->Accept(TEXT("McpAutomationBridgeClient"));
523
- if (ClientSocket) {
524
- // Create a new WebSocket instance for this client connection
525
- auto ClientWebSocket = MakeShared<FMcpBridgeWebSocket>(ClientSocket);
526
- ClientWebSocket->InitializeWeakSelf(ClientWebSocket);
527
- ClientWebSocket->bServerMode =
528
- false; // Client connections are not in server mode
529
- ClientWebSocket->bServerAcceptedConnection =
530
- true; // This is a server-accepted connection
531
- // Annotate the accepted client socket with the server listening port
532
- // so diagnostic logs and handshake acknowledgements report a
533
- // meaningful activePort instead of 0.
534
- ClientWebSocket->Port = Port;
535
-
536
- {
537
- FScopeLock Lock(&ClientSocketsMutex);
538
- ClientSockets.Add(ClientWebSocket);
539
- }
540
-
541
- TWeakPtr<FMcpBridgeWebSocket> LocalWeakThis = SelfWeakPtr;
542
- auto RemoveFromClientList = [LocalWeakThis, ClientWebSocket] {
543
- if (TSharedPtr<FMcpBridgeWebSocket> Pinned = LocalWeakThis.Pin()) {
544
- FScopeLock Lock(&Pinned->ClientSocketsMutex);
545
- UE_LOG(LogMcpAutomationBridgeSubsystem, VeryVerbose,
546
- TEXT("Removing client socket from server tracking (remaining "
547
- "before remove: %d)."),
548
- Pinned->ClientSockets.Num());
549
- Pinned->ClientSockets.Remove(ClientWebSocket);
550
- }
551
- };
552
-
553
- ClientWebSocket->OnConnected().AddLambda(
554
- [LocalWeakThis, ClientWebSocket](TSharedPtr<FMcpBridgeWebSocket>) {
555
- if (TSharedPtr<FMcpBridgeWebSocket> Pinned = LocalWeakThis.Pin()) {
556
- DispatchOnGameThread(
557
- [ParentWeak = LocalWeakThis, ClientSocket = ClientWebSocket] {
558
- if (TSharedPtr<FMcpBridgeWebSocket> DispatchPinned =
559
- ParentWeak.Pin()) {
560
- UE_LOG(LogMcpAutomationBridgeSubsystem, Log,
561
- TEXT("Broadcasting client connected delegate."));
562
- DispatchPinned->ClientConnectedDelegate.Broadcast(
563
- ClientSocket);
564
- }
565
- });
566
- }
567
- });
568
-
569
- ClientWebSocket->OnClosed().AddLambda(
570
- [RemoveFromClientList](TSharedPtr<FMcpBridgeWebSocket>, int32,
571
- const FString &,
572
- bool) { RemoveFromClientList(); });
573
-
574
- ClientWebSocket->OnConnectionError().AddLambda(
575
- [RemoveFromClientList](const FString &) { RemoveFromClientList(); });
576
-
577
- // Start the client WebSocket thread to handle the handshake and
578
- // communication
579
- ClientWebSocket->Connect();
580
- } else {
581
- // Sleep briefly to avoid busy waiting
582
- FPlatformProcess::Sleep(AcceptSleepSeconds > 0.0f ? AcceptSleepSeconds
583
- : 0.01f);
584
- }
585
- }
586
-
587
- if (ListenSocket) {
588
- ListenSocket->Close();
589
- SocketSubsystem->DestroySocket(ListenSocket);
590
- ListenSocket = nullptr;
591
- }
592
-
593
- return 0;
594
- }
595
-
596
- void FMcpBridgeWebSocket::Stop() {
597
- bStopping = true;
598
- if (StopEvent) {
599
- StopEvent->Trigger();
600
- }
601
- }
602
-
603
- void FMcpBridgeWebSocket::TearDown(const FString &Reason, bool bWasClean,
604
- int32 StatusCode) {
605
- if (FSocket *LocalSocket = DetachSocket()) {
606
- LocalSocket->Close();
607
- ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(LocalSocket);
608
- }
609
-
610
- const bool bWasConnected = bConnected;
611
- bConnected = false;
612
- ResetFragmentState();
613
-
614
- DispatchOnGameThread([WeakThis = SelfWeakPtr, Reason, bWasClean, StatusCode,
615
- bWasConnected] {
616
- if (TSharedPtr<FMcpBridgeWebSocket> Pinned = WeakThis.Pin()) {
617
- if (!bWasConnected) {
618
- Pinned->ConnectionErrorDelegate.Broadcast(Reason);
619
- }
620
- Pinned->ClosedDelegate.Broadcast(Pinned, StatusCode, Reason, bWasClean);
621
- }
622
- });
623
- }
624
-
625
- bool FMcpBridgeWebSocket::PerformHandshake() {
626
- FParsedWebSocketUrl ParsedUrl;
627
- FString ParseError;
628
- if (!ParseWebSocketUrl(Url, ParsedUrl, ParseError)) {
629
- TearDown(ParseError, false, 4000);
630
- return false;
631
- }
632
-
633
- HostHeader = ParsedUrl.Host;
634
- Port = ParsedUrl.Port;
635
- HandshakePath = ParsedUrl.PathWithQuery;
636
-
637
- TSharedPtr<FInternetAddr> Endpoint;
638
- if (!ResolveEndpoint(Endpoint) || !Endpoint.IsValid()) {
639
- TearDown(TEXT("Unable to resolve WebSocket host."), false, 4000);
640
- return false;
641
- }
642
-
643
- ISocketSubsystem *SocketSubsystem =
644
- ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM);
645
- Socket = SocketSubsystem->CreateSocket(
646
- NAME_Stream, TEXT("McpAutomationBridgeSocket"), false);
647
- if (!Socket) {
648
- TearDown(TEXT("Failed to create socket."), false, 4000);
649
- return false;
650
- }
651
- Socket->SetReuseAddr(true);
652
- Socket->SetNonBlocking(false);
653
- Socket->SetNoDelay(true);
654
-
655
- Endpoint->SetPort(Port);
656
- if (!Socket->Connect(*Endpoint)) {
657
- TearDown(TEXT("Unable to connect to WebSocket endpoint."), false, 4000);
658
- return false;
659
- }
660
-
661
- TArray<uint8> KeyBytes;
662
- KeyBytes.SetNumUninitialized(16);
663
- for (uint8 &Byte : KeyBytes) {
664
- Byte = static_cast<uint8>(FMath::RandRange(0, 255));
665
- }
666
- HandshakeKey = FBase64::Encode(KeyBytes.GetData(), KeyBytes.Num());
667
-
668
- FString HostLine = HostHeader;
669
- const bool bIsIpv6Host = HostLine.Contains(TEXT(":"));
670
- if (bIsIpv6Host && !HostLine.StartsWith(TEXT("["))) {
671
- HostLine = FString::Printf(TEXT("[%s]"), *HostLine);
672
- }
673
- if (!(Port == 80 || Port == 0)) {
674
- HostLine += FString::Printf(TEXT(":%d"), Port);
675
- }
676
-
677
- TStringBuilder<512> RequestBuilder;
678
- RequestBuilder << TEXT("GET ") << HandshakePath << TEXT(" HTTP/1.1\r\n");
679
- RequestBuilder << TEXT("Host: ") << HostLine << TEXT("\r\n");
680
- RequestBuilder << TEXT("Upgrade: websocket\r\n");
681
- RequestBuilder << TEXT("Connection: Upgrade\r\n");
682
- RequestBuilder << TEXT("Sec-WebSocket-Version: 13\r\n");
683
- RequestBuilder << TEXT("Sec-WebSocket-Key: ") << HandshakeKey << TEXT("\r\n");
684
-
685
- if (!Protocols.IsEmpty()) {
686
- RequestBuilder << TEXT("Sec-WebSocket-Protocol: ") << Protocols
687
- << TEXT("\r\n");
688
- }
689
-
690
- for (const TPair<FString, FString> &HeaderPair : Headers) {
691
- RequestBuilder << HeaderPair.Key << TEXT(": ") << HeaderPair.Value
692
- << TEXT("\r\n");
693
- }
694
-
695
- RequestBuilder << TEXT("\r\n");
696
-
697
- FTCHARToUTF8 HandshakeUtf8(RequestBuilder.ToString());
698
- int32 BytesSent = 0;
699
- if (!Socket->Send(reinterpret_cast<const uint8 *>(HandshakeUtf8.Get()),
700
- HandshakeUtf8.Length(), BytesSent) ||
701
- BytesSent != HandshakeUtf8.Length()) {
702
- TearDown(TEXT("Failed to send WebSocket handshake."), false, 4000);
703
- return false;
704
- }
705
-
706
- TArray<uint8> ResponseBuffer;
707
- ResponseBuffer.Reserve(512);
708
- constexpr int32 TempSize = 256;
709
- uint8 Temp[TempSize];
710
- bool bHandshakeComplete = false;
711
- while (!bHandshakeComplete) {
712
- if (bStopping) {
713
- return false;
714
- }
715
- int32 BytesRead = 0;
716
- if (!Socket->Recv(Temp, TempSize, BytesRead)) {
717
- TearDown(TEXT("WebSocket handshake failed while reading response."),
718
- false, 4000);
719
- return false;
720
- }
721
- if (BytesRead <= 0) {
722
- continue;
723
- }
724
- ResponseBuffer.Append(Temp, BytesRead);
725
- if (ResponseBuffer.Num() >= 4) {
726
- const int32 Count = ResponseBuffer.Num();
727
- if (ResponseBuffer[Count - 4] == '\r' &&
728
- ResponseBuffer[Count - 3] == '\n' &&
729
- ResponseBuffer[Count - 2] == '\r' &&
730
- ResponseBuffer[Count - 1] == '\n') {
731
- bHandshakeComplete = true;
732
- }
733
- }
734
- }
735
-
736
- FString ResponseString = FString(
737
- ANSI_TO_TCHAR(reinterpret_cast<const char *>(ResponseBuffer.GetData())));
738
- FString HeaderSection;
739
- FString ExtraData;
740
- if (!ResponseString.Split(TEXT("\r\n\r\n"), &HeaderSection, &ExtraData)) {
741
- HeaderSection = ResponseString;
742
- }
743
-
744
- TArray<FString> HeaderLines;
745
- HeaderSection.ParseIntoArrayLines(HeaderLines, false);
746
- if (HeaderLines.Num() == 0) {
747
- TearDown(TEXT("Malformed WebSocket handshake response."), false, 4000);
748
- return false;
749
- }
750
-
751
- const FString &StatusLine = HeaderLines[0];
752
- if (!StatusLine.Contains(TEXT("101"))) {
753
- TearDown(TEXT("WebSocket server rejected handshake."), false, 4000);
754
- return false;
755
- }
756
-
757
- FString ExpectedAccept;
758
- {
759
- FTCHARToUTF8 AcceptUtf8(*(HandshakeKey + WebSocketGuid));
760
- FSHA1 Hash;
761
- Hash.Update(reinterpret_cast<const uint8 *>(AcceptUtf8.Get()),
762
- AcceptUtf8.Length());
763
- Hash.Final();
764
- uint8 Digest[FSHA1::DigestSize];
765
- Hash.GetHash(Digest);
766
- ExpectedAccept = FBase64::Encode(Digest, FSHA1::DigestSize);
767
- }
768
-
769
- bool bAcceptValid = false;
770
- for (int32 i = 1; i < HeaderLines.Num(); ++i) {
771
- FString Key;
772
- FString Value;
773
- if (HeaderLines[i].Split(TEXT(":"), &Key, &Value)) {
774
- Key = Key.TrimStartAndEnd();
775
- Value = Value.TrimStartAndEnd();
776
- if (Key.Equals(TEXT("Sec-WebSocket-Accept"), ESearchCase::IgnoreCase)) {
777
- bAcceptValid = Value.Equals(ExpectedAccept, ESearchCase::CaseSensitive);
778
- }
779
- }
780
- }
781
-
782
- if (!bAcceptValid) {
783
- TearDown(TEXT("WebSocket handshake validation failed."), false, 4000);
784
- return false;
785
- }
786
-
787
- if (!ExtraData.IsEmpty()) {
788
- const FTCHARToUTF8 ExtraUtf8(*ExtraData);
789
- PendingReceived.Append(reinterpret_cast<const uint8 *>(ExtraUtf8.Get()),
790
- ExtraUtf8.Length());
791
- }
792
-
793
- return true;
794
- }
795
-
796
- bool FMcpBridgeWebSocket::PerformServerHandshake() {
797
- // Read the client's WebSocket upgrade request
798
- TArray<uint8> RequestBuffer;
799
- RequestBuffer.Reserve(1024);
800
- constexpr int32 TempSize = 256;
801
- uint8 Temp[TempSize];
802
- bool bRequestComplete = false;
803
- FString ClientKey;
804
-
805
- int32 HeaderEndIndex = -1;
806
- while (!bRequestComplete) {
807
- if (bStopping) {
808
- return false;
809
- }
810
-
811
- int32 BytesRead = 0;
812
- if (!Socket->Recv(Temp, TempSize, BytesRead)) {
813
- // This may occur when a client connects but immediately closes
814
- // or when a non-WebSocket probe connects; log at Verbose to avoid
815
- // spamming warnings for transient or benign network activity.
816
- UE_LOG(LogMcpAutomationBridgeSubsystem, Verbose,
817
- TEXT("Server handshake recv failed while awaiting upgrade request "
818
- "(benign or client closed)."));
819
- TearDown(TEXT("Failed to read WebSocket upgrade request."), false, 4000);
820
- return false;
821
- }
822
-
823
- if (BytesRead <= 0) {
824
- continue;
825
- }
826
-
827
- RequestBuffer.Append(Temp, BytesRead);
828
-
829
- // Check if we have a complete HTTP request (double CRLF) anywhere
830
- // in the buffer. Clients may send additional bytes immediately after
831
- // the headers (for example, the first WebSocket frame), so search
832
- // the whole buffer and capture any trailing bytes beyond the header
833
- // terminator into PendingReceived for the frame parser.
834
- if (RequestBuffer.Num() >= 4) {
835
- for (int32 Idx = 0; Idx + 3 < RequestBuffer.Num(); ++Idx) {
836
- if (RequestBuffer[Idx] == '\r' && RequestBuffer[Idx + 1] == '\n' &&
837
- RequestBuffer[Idx + 2] == '\r' && RequestBuffer[Idx + 3] == '\n') {
838
- HeaderEndIndex = Idx + 4;
839
- bRequestComplete = true;
840
- break;
841
- }
842
- }
843
- }
844
- }
845
-
846
- FString RequestString = FString(
847
- ANSI_TO_TCHAR(reinterpret_cast<const char *>(RequestBuffer.GetData())));
848
- TArray<FString> RequestLines;
849
- RequestString.ParseIntoArrayLines(RequestLines, false);
850
-
851
- if (RequestLines.Num() == 0) {
852
- UE_LOG(LogMcpAutomationBridgeSubsystem, Warning,
853
- TEXT("Server handshake received empty upgrade request."));
854
- TearDown(TEXT("Malformed WebSocket upgrade request."), false, 4000);
855
- return false;
856
- }
857
-
858
- // If there were any bytes received after the HTTP header terminator,
859
- // preserve them so the frame parser can consume an arriving WebSocket
860
- // frame that arrived in the same TCP packet as the upgrade request.
861
- if (HeaderEndIndex > 0 && HeaderEndIndex < RequestBuffer.Num()) {
862
- const int32 ExtraCount = RequestBuffer.Num() - HeaderEndIndex;
863
- if (ExtraCount > 0) {
864
- FScopeLock Guard(&ReceiveMutex);
865
- PendingReceived.Append(RequestBuffer.GetData() + HeaderEndIndex,
866
- ExtraCount);
867
- UE_LOG(LogMcpAutomationBridgeSubsystem, Verbose,
868
- TEXT("Server handshake: preserved %d extra bytes after upgrade "
869
- "request for subsequent frame parsing."),
870
- ExtraCount);
871
- }
872
- }
873
-
874
- // Parse the request
875
- bool bValidUpgrade = false;
876
- bool bValidConnection = false;
877
- bool bValidVersion = false;
878
- FString RequestedProtocols;
879
-
880
- for (int32 i = 1; i < RequestLines.Num(); ++i) {
881
- FString Key, Value;
882
- if (RequestLines[i].Split(TEXT(":"), &Key, &Value)) {
883
- Key = Key.TrimStartAndEnd();
884
- Value = Value.TrimStartAndEnd();
885
-
886
- if (Key.Equals(TEXT("Upgrade"), ESearchCase::IgnoreCase) &&
887
- Value.Equals(TEXT("websocket"), ESearchCase::IgnoreCase)) {
888
- bValidUpgrade = true;
889
- } else if (Key.Equals(TEXT("Connection"), ESearchCase::IgnoreCase) &&
890
- Value.Equals(TEXT("Upgrade"), ESearchCase::IgnoreCase)) {
891
- bValidConnection = true;
892
- } else if (Key.Equals(TEXT("Sec-WebSocket-Version"),
893
- ESearchCase::IgnoreCase) &&
894
- Value.Equals(TEXT("13"), ESearchCase::CaseSensitive)) {
895
- bValidVersion = true;
896
- } else if (Key.Equals(TEXT("Sec-WebSocket-Key"),
897
- ESearchCase::IgnoreCase)) {
898
- ClientKey = Value;
899
- } else if (Key.Equals(TEXT("Sec-WebSocket-Protocol"),
900
- ESearchCase::IgnoreCase)) {
901
- RequestedProtocols = Value;
902
- }
903
- }
904
- }
905
-
906
- if (!bValidUpgrade || !bValidConnection || !bValidVersion ||
907
- ClientKey.IsEmpty()) {
908
- UE_LOG(LogMcpAutomationBridgeSubsystem, Warning,
909
- TEXT("Server handshake validation failed (upgrade=%s, "
910
- "connection=%s, version=%s, hasKey=%s)."),
911
- bValidUpgrade ? TEXT("true") : TEXT("false"),
912
- bValidConnection ? TEXT("true") : TEXT("false"),
913
- bValidVersion ? TEXT("true") : TEXT("false"),
914
- ClientKey.IsEmpty() ? TEXT("false") : TEXT("true"));
915
- TearDown(TEXT("Invalid WebSocket upgrade request."), false, 4000);
916
- return false;
917
- }
918
-
919
- // Generate the accept key
920
- const FString AcceptGuid = TEXT("258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
921
- FString AcceptKey;
922
- {
923
- FTCHARToUTF8 AcceptUtf8(*(ClientKey + AcceptGuid));
924
- FSHA1 Hash;
925
- Hash.Update(reinterpret_cast<const uint8 *>(AcceptUtf8.Get()),
926
- AcceptUtf8.Length());
927
- Hash.Final();
928
- uint8 Digest[FSHA1::DigestSize];
929
- Hash.GetHash(Digest);
930
- AcceptKey = FBase64::Encode(Digest, FSHA1::DigestSize);
931
- }
932
-
933
- FString SelectedProtocol;
934
- if (!Protocols.IsEmpty() && !RequestedProtocols.IsEmpty()) {
935
- TArray<FString> RequestedList;
936
- RequestedProtocols.ParseIntoArray(RequestedList, TEXT(","), true);
937
-
938
- TArray<FString> SupportedList;
939
- Protocols.ParseIntoArray(SupportedList, TEXT(","), true);
940
-
941
- for (const FString &Requested : RequestedList) {
942
- const FString TrimmedRequested = Requested.TrimStartAndEnd();
943
- for (const FString &Supported : SupportedList) {
944
- if (TrimmedRequested.Equals(Supported.TrimStartAndEnd(),
945
- ESearchCase::IgnoreCase)) {
946
- SelectedProtocol = Supported.TrimStartAndEnd();
947
- break;
948
- }
949
- }
950
- if (!SelectedProtocol.IsEmpty()) {
951
- break;
952
- }
953
- }
954
- }
955
-
956
- if (!RequestedProtocols.IsEmpty() && SelectedProtocol.IsEmpty()) {
957
- UE_LOG(LogMcpAutomationBridgeSubsystem, Warning,
958
- TEXT("Server handshake failed: no matching subprotocol. "
959
- "Requested=%s Supported=%s"),
960
- *RequestedProtocols, *Protocols);
961
- TearDown(TEXT("No matching WebSocket subprotocol."), false, 4403);
962
- return false;
963
- }
964
-
965
- // Send the upgrade response
966
- FString Response = FString::Printf(TEXT("HTTP/1.1 101 Switching Protocols\r\n"
967
- "Upgrade: websocket\r\n"
968
- "Connection: Upgrade\r\n"
969
- "Sec-WebSocket-Accept: %s\r\n"),
970
- *AcceptKey);
971
-
972
- if (!SelectedProtocol.IsEmpty()) {
973
- Response += FString::Printf(TEXT("Sec-WebSocket-Protocol: %s\r\n"),
974
- *SelectedProtocol);
975
- }
976
-
977
- Response += TEXT("\r\n");
978
-
979
- FTCHARToUTF8 ResponseUtf8(*Response);
980
- int32 BytesSent = 0;
981
- if (!Socket->Send(reinterpret_cast<const uint8 *>(ResponseUtf8.Get()),
982
- ResponseUtf8.Length(), BytesSent) ||
983
- BytesSent != ResponseUtf8.Length()) {
984
- UE_LOG(LogMcpAutomationBridgeSubsystem, Warning,
985
- TEXT("Server handshake failed: unable to send upgrade response "
986
- "(sent %d expected %d)."),
987
- BytesSent, ResponseUtf8.Length());
988
- TearDown(TEXT("Failed to send WebSocket upgrade response."), false, 4000);
989
- return false;
990
- }
991
-
992
- UE_LOG(LogMcpAutomationBridgeSubsystem, Log,
993
- TEXT("Server handshake completed; subprotocol=%s"),
994
- SelectedProtocol.IsEmpty() ? TEXT("(none)") : *SelectedProtocol);
995
-
996
- return true;
997
- }
998
-
999
- bool FMcpBridgeWebSocket::ResolveEndpoint(TSharedPtr<FInternetAddr> &OutAddr) {
1000
- ISocketSubsystem *SocketSubsystem =
1001
- ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM);
1002
- if (!SocketSubsystem) {
1003
- return false;
1004
- }
1005
-
1006
- const FString ServiceName = FString::FromInt(Port);
1007
- FAddressInfoResult AddrInfo = SocketSubsystem->GetAddressInfo(
1008
- *HostHeader, *ServiceName, EAddressInfoFlags::Default, NAME_None,
1009
- ESocketType::SOCKTYPE_Streaming);
1010
- if (AddrInfo.Results.Num() == 0) {
1011
- return false;
1012
- }
1013
-
1014
- OutAddr = AddrInfo.Results[0].Address;
1015
- if (OutAddr.IsValid()) {
1016
- OutAddr->SetPort(Port);
1017
- }
1018
- return OutAddr.IsValid();
1019
- }
1020
-
1021
- bool FMcpBridgeWebSocket::SendFrame(const TArray<uint8> &Frame) {
1022
- if (!Socket) {
1023
- return false;
1024
- }
1025
-
1026
- int32 TotalBytesSent = 0;
1027
- const int32 TotalBytesToSend = Frame.Num();
1028
-
1029
- while (TotalBytesSent < TotalBytesToSend) {
1030
- int32 BytesSent = 0;
1031
- if (!Socket->Send(Frame.GetData() + TotalBytesSent,
1032
- TotalBytesToSend - TotalBytesSent, BytesSent)) {
1033
- UE_LOG(LogMcpAutomationBridgeSubsystem, Error,
1034
- TEXT("Socket Send failed after sending %d / %d bytes"),
1035
- TotalBytesSent, TotalBytesToSend);
1036
- return false;
1037
- }
1038
-
1039
- if (BytesSent <= 0) {
1040
- UE_LOG(LogMcpAutomationBridgeSubsystem, Error,
1041
- TEXT("Socket Send returned %d bytes (expected > 0). Closing "
1042
- "connection."),
1043
- BytesSent);
1044
- return false;
1045
- }
1046
-
1047
- TotalBytesSent += BytesSent;
1048
- }
1049
-
1050
- return true;
1051
- }
1052
-
1053
- bool FMcpBridgeWebSocket::SendCloseFrame(int32 StatusCode,
1054
- const FString &Reason) {
1055
- TArray<uint8> Payload;
1056
- Payload.Reserve(2 + Reason.Len() * 4);
1057
-
1058
- const uint16 Code = ToNetwork16(static_cast<uint16>(StatusCode));
1059
- Payload.Append(reinterpret_cast<const uint8 *>(&Code), sizeof(uint16));
1060
-
1061
- FTCHARToUTF8 ReasonUtf8(*Reason);
1062
- const int32 ReasonBytes = FMath::Min<int32>(
1063
- ReasonUtf8.Length(),
1064
- 123); // ensure control frame payload stays within 125 bytes
1065
- if (ReasonBytes > 0) {
1066
- Payload.Append(reinterpret_cast<const uint8 *>(ReasonUtf8.Get()),
1067
- ReasonBytes);
1068
- }
1069
-
1070
- return SendControlFrame(OpCodeClose, Payload);
1071
- }
1072
-
1073
- bool FMcpBridgeWebSocket::SendTextFrame(const void *Data, SIZE_T Length) {
1074
- const uint8 *Raw = static_cast<const uint8 *>(Data);
1075
- TArray<uint8> Frame;
1076
-
1077
- const uint8 Header = 0x80 | OpCodeText;
1078
- Frame.Add(Header);
1079
-
1080
- const bool bMask = !bServerAcceptedConnection;
1081
-
1082
- if (Length <= 125) {
1083
- Frame.Add((bMask ? 0x80 : 0x00) | static_cast<uint8>(Length));
1084
- } else if (Length <= 0xFFFF) {
1085
- Frame.Add((bMask ? 0x80 : 0x00) | 126);
1086
- const uint16 SizeShort = ToNetwork16(static_cast<uint16>(Length));
1087
- Frame.Append(reinterpret_cast<const uint8 *>(&SizeShort), sizeof(uint16));
1088
- } else {
1089
- Frame.Add((bMask ? 0x80 : 0x00) | 127);
1090
- const uint64 SizeLong = ToNetwork64(static_cast<uint64>(Length));
1091
- Frame.Append(reinterpret_cast<const uint8 *>(&SizeLong), sizeof(uint64));
1092
- }
1093
-
1094
- if (bMask) {
1095
- uint8 MaskKey[4];
1096
- for (uint8 &Byte : MaskKey) {
1097
- Byte = static_cast<uint8>(FMath::RandRange(0, 255));
1098
- }
1099
- Frame.Append(MaskKey, 4);
1100
-
1101
- const int64 Offset = Frame.Num();
1102
- Frame.AddUninitialized(Length);
1103
- for (SIZE_T Index = 0; Index < Length; ++Index) {
1104
- Frame[Offset + Index] = Raw[Index] ^ MaskKey[Index % 4];
1105
- }
1106
- } else {
1107
- Frame.Append(Raw, static_cast<int32>(Length));
1108
- }
1109
-
1110
- FScopeLock Guard(&SendMutex);
1111
- return SendFrame(Frame);
1112
- }
1113
-
1114
- bool FMcpBridgeWebSocket::SendControlFrame(const uint8 ControlOpCode,
1115
- const TArray<uint8> &Payload) {
1116
- if (!Socket) {
1117
- return false;
1118
- }
1119
-
1120
- if (Payload.Num() > 125) {
1121
- return false;
1122
- }
1123
-
1124
- FScopeLock Guard(&SendMutex);
1125
-
1126
- TArray<uint8> Frame;
1127
- Frame.Reserve(2 + 4 + Payload.Num());
1128
- Frame.Add(0x80 | (ControlOpCode & 0x0F));
1129
- const bool bMask = !bServerAcceptedConnection;
1130
- Frame.Add((bMask ? 0x80 : 0x00) | static_cast<uint8>(Payload.Num()));
1131
-
1132
- if (bMask) {
1133
- uint8 MaskKey[4];
1134
- for (uint8 &Byte : MaskKey) {
1135
- Byte = static_cast<uint8>(FMath::RandRange(0, 255));
1136
- }
1137
-
1138
- Frame.Append(MaskKey, 4);
1139
- const int32 PayloadOffset = Frame.Num();
1140
- Frame.AddUninitialized(Payload.Num());
1141
- for (int32 Index = 0; Index < Payload.Num(); ++Index) {
1142
- Frame[PayloadOffset + Index] = Payload[Index] ^ MaskKey[Index % 4];
1143
- }
1144
- } else if (Payload.Num() > 0) {
1145
- Frame.Append(Payload);
1146
- }
1147
-
1148
- return SendFrame(Frame);
1149
- }
1150
-
1151
- void FMcpBridgeWebSocket::HandleTextPayload(const TArray<uint8> &Payload) {
1152
- const FString Message = BytesToStringView(Payload);
1153
- // Dispatch message handling to the game thread.
1154
- // Many automation handlers touch editor/world state and must run on the
1155
- // game thread. Keeping the socket receive loop thread-free also prevents
1156
- // long-running actions (e.g. export_level) from stalling the connection.
1157
- DispatchOnGameThread([WeakThis = SelfWeakPtr, Message] {
1158
- if (TSharedPtr<FMcpBridgeWebSocket> Pinned = WeakThis.Pin()) {
1159
- Pinned->MessageDelegate.Broadcast(Pinned, Message);
1160
- }
1161
- });
1162
- }
1163
-
1164
- void FMcpBridgeWebSocket::ResetFragmentState() {
1165
- FragmentAccumulator.Reset();
1166
- bFragmentMessageActive = false;
1167
- }
1168
-
1169
- bool FMcpBridgeWebSocket::ReceiveFrame() {
1170
- uint8 Header[2];
1171
- if (!ReceiveExact(Header, 2)) {
1172
- TearDown(TEXT("Failed to read WebSocket frame header."), false, 4001);
1173
- return false;
1174
- }
1175
-
1176
- const bool bFinalFrame = (Header[0] & 0x80) != 0;
1177
- const uint8 OpCode = Header[0] & 0x0F;
1178
- uint64 PayloadLength = Header[1] & 0x7F;
1179
- const bool bMasked = (Header[1] & 0x80) != 0;
1180
-
1181
- if (PayloadLength == 126) {
1182
- uint8 Extended[2];
1183
- if (!ReceiveExact(Extended, sizeof(Extended))) {
1184
- TearDown(TEXT("Failed to read extended payload length."), false, 4001);
1185
- return false;
1186
- }
1187
- uint16 ShortVal = 0;
1188
- FMemory::Memcpy(&ShortVal, Extended, sizeof(uint16));
1189
- PayloadLength = FromNetwork16(ShortVal);
1190
- } else if (PayloadLength == 127) {
1191
- uint8 Extended[8];
1192
- if (!ReceiveExact(Extended, sizeof(Extended))) {
1193
- TearDown(TEXT("Failed to read extended payload length."), false, 4001);
1194
- return false;
1195
- }
1196
- uint64 LongVal = 0;
1197
- FMemory::Memcpy(&LongVal, Extended, sizeof(uint64));
1198
- PayloadLength = FromNetwork64(LongVal);
1199
- }
1200
-
1201
- uint8 MaskKey[4] = {0, 0, 0, 0};
1202
- if (bMasked) {
1203
- if (!ReceiveExact(MaskKey, 4)) {
1204
- TearDown(TEXT("Failed to read masking key."), false, 4001);
1205
- return false;
1206
- }
1207
- }
1208
-
1209
- TArray<uint8> Payload;
1210
- if (PayloadLength > 0) {
1211
- Payload.SetNumUninitialized(static_cast<int32>(PayloadLength));
1212
- if (!ReceiveExact(Payload.GetData(), PayloadLength)) {
1213
- TearDown(TEXT("Failed to read WebSocket payload."), false, 4001);
1214
- return false;
1215
- }
1216
- if (bMasked) {
1217
- for (uint64 Index = 0; Index < PayloadLength; ++Index) {
1218
- Payload[Index] ^= MaskKey[Index % 4];
1219
- }
1220
- }
1221
- }
1222
-
1223
- if (OpCode == OpCodeClose) {
1224
- TearDown(TEXT("WebSocket closed by peer."), true, 1000);
1225
- return false;
1226
- }
1227
-
1228
- // Handle control frames immediately (they must not be fragmented)
1229
- if ((OpCode & 0x08) != 0) {
1230
- if (!bFinalFrame) {
1231
- TearDown(TEXT("Control frames must not be fragmented."), false, 4002);
1232
- return false;
1233
- }
1234
-
1235
- if (OpCode == OpCodePing) {
1236
- SendControlFrame(OpCodePong, Payload);
1237
- return true;
1238
- }
1239
-
1240
- if (OpCode == OpCodePong) {
1241
- // In server mode, receiving a pong means the client is responding to our
1242
- // ping In client mode, receiving a pong means the server responded to our
1243
- // ping
1244
- HeartbeatDelegate.Broadcast(SelfWeakPtr.Pin());
1245
- return true;
1246
- }
1247
-
1248
- // Unknown control frame
1249
- return true;
1250
- }
1251
-
1252
- if (OpCode == OpCodeContinuation) {
1253
- if (!bFragmentMessageActive) {
1254
- TearDown(TEXT("Unexpected continuation frame."), false, 4002);
1255
- return false;
1256
- }
1257
-
1258
- FragmentAccumulator.Append(Payload);
1259
-
1260
- if (bFinalFrame) {
1261
- HandleTextPayload(FragmentAccumulator);
1262
- ResetFragmentState();
1263
- }
1264
- return true;
1265
- }
1266
-
1267
- if (bFragmentMessageActive) {
1268
- TearDown(
1269
- TEXT("Received new data frame before completing fragmented message."),
1270
- false, 4002);
1271
- return false;
1272
- }
1273
-
1274
- if (OpCode == OpCodeText) {
1275
- if (bFinalFrame) {
1276
- HandleTextPayload(Payload);
1277
- } else {
1278
- FragmentAccumulator = Payload;
1279
- bFragmentMessageActive = true;
1280
- }
1281
- return true;
1282
- }
1283
-
1284
- if (OpCode == OpCodeBinary) {
1285
- TearDown(TEXT("Binary frames are not supported."), false, 4003);
1286
- return false;
1287
- }
1288
-
1289
- TearDown(TEXT("Unsupported WebSocket opcode."), false, 4003);
1290
- return false;
1291
- }
1292
-
1293
- bool FMcpBridgeWebSocket::ReceiveExact(uint8 *Buffer, SIZE_T Length) {
1294
- SIZE_T Collected = 0;
1295
-
1296
- {
1297
- FScopeLock Guard(&ReceiveMutex);
1298
- const SIZE_T Existing =
1299
- FMath::Min(static_cast<SIZE_T>(PendingReceived.Num()), Length);
1300
- if (Existing > 0) {
1301
- FMemory::Memcpy(Buffer, PendingReceived.GetData(), Existing);
1302
- PendingReceived.RemoveAt(0, Existing, EAllowShrinking::No);
1303
- Collected += Existing;
1304
- }
1305
- }
1306
-
1307
- while (Collected < Length) {
1308
- if (bStopping) {
1309
- return false;
1310
- }
1311
-
1312
- uint32 PendingSize = 0;
1313
- if (!Socket->HasPendingData(PendingSize)) {
1314
- if (StopEvent && StopEvent->Wait(FTimespan::FromMilliseconds(50))) {
1315
- return false;
1316
- }
1317
- continue;
1318
- }
1319
-
1320
- const uint32 ReadSize = FMath::Min<uint32>(PendingSize, 4096);
1321
- TArray<uint8> Temp;
1322
- Temp.SetNumUninitialized(ReadSize);
1323
- int32 BytesRead = 0;
1324
- if (!Socket->Recv(Temp.GetData(), ReadSize, BytesRead)) {
1325
- return false;
1326
- }
1327
-
1328
- if (BytesRead <= 0) {
1329
- continue;
1330
- }
1331
-
1332
- const uint32 CopyCount =
1333
- FMath::Min<uint32>(static_cast<uint32>(BytesRead),
1334
- static_cast<uint32>(Length - Collected));
1335
- FMemory::Memcpy(Buffer + Collected, Temp.GetData(), CopyCount);
1336
- Collected += CopyCount;
1337
-
1338
- if (static_cast<uint32>(BytesRead) > CopyCount) {
1339
- FScopeLock Guard(&ReceiveMutex);
1340
- PendingReceived.Append(Temp.GetData() + CopyCount, BytesRead - CopyCount);
1341
- }
1342
- }
1343
-
1344
- return true;
1345
- }