Skip to content

Commit 3e9cb30

Browse files
committed
MCP: improve ChatGPT compatibility
1 parent 8d7b8fd commit 3e9cb30

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

platforms/shared/desktop/mcp/mcp_debug_adapter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ std::vector<BreakpointInfo> DebugAdapter::ListBreakpoints()
170170
HuC6280* cpu = m_core->GetHuC6280();
171171
std::vector<HuC6280::GG_Breakpoint>* breakpoints = cpu->GetBreakpoints();
172172

173-
for (const auto& brk : *breakpoints)
173+
for (const HuC6280::GG_Breakpoint& brk : *breakpoints)
174174
{
175175
BreakpointInfo info;
176176
info.enabled = brk.enabled;

platforms/shared/desktop/mcp/mcp_manager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class McpManager
6565
if (m_server && m_server->IsRunning())
6666
return;
6767

68+
m_commandQueue.Clear();
69+
m_responseQueue.Reset();
70+
6871
McpTransportInterface* transport = NULL;
6972
if (m_transport_mode == MCP_TRANSPORT_TCP)
7073
{

platforms/shared/desktop/mcp/mcp_server.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ void McpServer::ReaderLoop()
5454

5555
void McpServer::Run()
5656
{
57-
// Start reader thread for stdin
57+
// Start reader thread for transport input
5858
std::thread reader_thread(ReaderThreadFunc, this);
5959
reader_thread.detach();
6060

61-
// Main loop: wait for responses and send them
61+
// Transport thread waits for responses generated by the main thread
62+
// (via PumpCommands) and sends them back through the transport
6263
while (m_running.load())
6364
{
6465
DebugResponse* resp = m_responseQueue.WaitAndPop();
@@ -144,7 +145,11 @@ void McpServer::HandleLine(const std::string& line)
144145
}
145146
else if (method == "notifications/initialized")
146147
{
147-
// No response required for notifications
148+
// Notifications don't need JSON-RPC response, but HTTP needs HTTP response
149+
if (dynamic_cast<HttpTransport*>(m_transport))
150+
{
151+
m_transport->send("{}");
152+
}
148153
}
149154
else if (method == "tools/list")
150155
{
@@ -1874,7 +1879,7 @@ void McpServer::LoadResourcesFromCategory(const std::string& category, const std
18741879
// Get the directory containing toc.json
18751880
std::string tocDir = tocPath.substr(0, tocPath.find_last_of("/\\"));
18761881

1877-
for (const auto& item : toc["toc"])
1882+
for (const json& item : toc["toc"])
18781883
{
18791884
if (!item.contains("uri") || !item.contains("title"))
18801885
continue;
@@ -1918,7 +1923,7 @@ void McpServer::HandleResourcesList(const json& request)
19181923

19191924
json resources = json::array();
19201925

1921-
for (const auto& resource : m_resources)
1926+
for (const ResourceInfo& resource : m_resources)
19221927
{
19231928
json resourceJson;
19241929
resourceJson["uri"] = resource.uri;
@@ -1958,7 +1963,7 @@ void McpServer::HandleResourcesRead(const json& request)
19581963
std::string uri = request["params"]["uri"];
19591964

19601965
// Find resource
1961-
auto it = m_resourceMap.find(uri);
1966+
std::map<std::string, ResourceInfo>::const_iterator it = m_resourceMap.find(uri);
19621967
if (it == m_resourceMap.end())
19631968
{
19641969
SendError(id, -32602, "Resource not found: " + uri);

platforms/shared/desktop/mcp/mcp_server.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ class CommandQueue
7979
return cmd;
8080
}
8181

82+
void Clear()
83+
{
84+
std::lock_guard<std::mutex> lock(m_mutex);
85+
while (!m_queue.empty())
86+
{
87+
SafeDelete(m_queue.front());
88+
m_queue.pop();
89+
}
90+
}
91+
8292
private:
8393
std::queue<DebugCommand*> m_queue;
8494
std::mutex m_mutex;
@@ -114,6 +124,17 @@ class ResponseQueue
114124
m_cv.notify_all();
115125
}
116126

127+
void Reset()
128+
{
129+
std::lock_guard<std::mutex> lock(m_mutex);
130+
while (!m_queue.empty())
131+
{
132+
SafeDelete(m_queue.front());
133+
m_queue.pop();
134+
}
135+
m_running = true;
136+
}
137+
117138
private:
118139
std::queue<DebugResponse*> m_queue;
119140
std::mutex m_mutex;

platforms/shared/desktop/mcp/mcp_transport.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <string>
2626
#include <iostream>
2727
#include <mutex>
28+
#include <condition_variable>
2829
#include <cerrno>
2930
#include "log.h"
3031

@@ -178,6 +179,7 @@ class HttpTransport : public McpTransportInterface
178179
Error("[MCP] HTTP send() failed: %d", sent);
179180
SOCKET_CLOSE(m_current_client);
180181
m_current_client = INVALID_SOCKET_VALUE;
182+
m_client_cv.notify_all();
181183
return false;
182184
}
183185

@@ -190,6 +192,7 @@ class HttpTransport : public McpTransportInterface
190192
// Close connection after response (VS Code will reconnect for next request)
191193
SOCKET_CLOSE(m_current_client);
192194
m_current_client = INVALID_SOCKET_VALUE;
195+
m_client_cv.notify_all();
193196

194197
return true;
195198
}
@@ -232,6 +235,7 @@ class HttpTransport : public McpTransportInterface
232235
{
233236
std::lock_guard<std::mutex> lock(m_mutex);
234237
m_current_client = INVALID_SOCKET_VALUE;
238+
m_client_cv.notify_all();
235239
}
236240
return false;
237241
}
@@ -240,7 +244,22 @@ class HttpTransport : public McpTransportInterface
240244
{
241245
while (!m_closed && m_server_socket != INVALID_SOCKET_VALUE)
242246
{
243-
// Always accept new connection (we close after each response)
247+
// Wait until any previous client has been responded to and closed
248+
{
249+
std::unique_lock<std::mutex> lock(m_mutex);
250+
m_client_cv.wait(lock, [this]
251+
{
252+
return m_closed || m_current_client == INVALID_SOCKET_VALUE;
253+
});
254+
255+
if (m_closed)
256+
{
257+
Debug("[MCP] Server closed, stopping recv loop");
258+
return false;
259+
}
260+
}
261+
262+
// Accept new connection (we close after each response)
244263
struct sockaddr_in client_addr;
245264
socklen_t client_len = sizeof(client_addr);
246265
socket_t client = accept(m_server_socket, (struct sockaddr*)&client_addr, &client_len);
@@ -346,6 +365,7 @@ class HttpTransport : public McpTransportInterface
346365
std::lock_guard<std::mutex> lock(m_mutex);
347366
SOCKET_CLOSE(m_current_client);
348367
m_current_client = INVALID_SOCKET_VALUE;
368+
m_client_cv.notify_all();
349369
continue; // Wait for next connection
350370
}
351371

@@ -367,6 +387,7 @@ class HttpTransport : public McpTransportInterface
367387
std::lock_guard<std::mutex> lock(m_mutex);
368388
SOCKET_CLOSE(m_current_client);
369389
m_current_client = INVALID_SOCKET_VALUE;
390+
m_client_cv.notify_all();
370391
continue; // Wait for next connection
371392
}
372393

@@ -406,6 +427,7 @@ class HttpTransport : public McpTransportInterface
406427
std::lock_guard<std::mutex> lock(m_mutex);
407428
SOCKET_CLOSE(m_current_client);
408429
m_current_client = INVALID_SOCKET_VALUE;
430+
m_client_cv.notify_all();
409431
continue; // Wait for next request
410432
}
411433

@@ -421,6 +443,7 @@ class HttpTransport : public McpTransportInterface
421443
std::lock_guard<std::mutex> lock(m_mutex);
422444
SOCKET_CLOSE(m_current_client);
423445
m_current_client = INVALID_SOCKET_VALUE;
446+
m_client_cv.notify_all();
424447
continue; // Wait for next request
425448
}
426449

@@ -439,6 +462,7 @@ class HttpTransport : public McpTransportInterface
439462
std::lock_guard<std::mutex> lock(m_mutex);
440463
SOCKET_CLOSE(m_current_client);
441464
m_current_client = INVALID_SOCKET_VALUE;
465+
m_client_cv.notify_all();
442466
continue; // Wait for next connection
443467
}
444468
}
@@ -449,6 +473,7 @@ class HttpTransport : public McpTransportInterface
449473
void close()
450474
{
451475
m_closed = true;
476+
m_client_cv.notify_all();
452477

453478
if (m_current_client != INVALID_SOCKET_VALUE)
454479
{
@@ -465,6 +490,7 @@ class HttpTransport : public McpTransportInterface
465490

466491
private:
467492
std::mutex m_mutex;
493+
std::condition_variable m_client_cv;
468494
bool m_closed;
469495
socket_t m_server_socket;
470496
socket_t m_current_client;

0 commit comments

Comments
 (0)