-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmcp_inprocess.cpp
More file actions
309 lines (263 loc) · 11.2 KB
/
mcp_inprocess.cpp
File metadata and controls
309 lines (263 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright (c) 2025 Elias Bachaalany
// SPDX-License-Identifier: MIT
/// @file mcp_inprocess.cpp
/// @brief In-process Copilot SDK + fastmcpp MCP server integration example
///
/// This example demonstrates running a fastmcpp MCP server and Copilot SDK
/// client in the same process. The fastmcpp server provides custom tools
/// that Copilot can invoke.
///
/// Architecture:
/// - fastmcpp SSE server runs in a background thread on localhost:18080
/// - Copilot SDK connects to it as a remote MCP server
/// - Copilot can invoke tools defined in the fastmcpp server
///
/// Build:
/// cmake -B build -DCOPILOT_WITH_FASTMCPP=ON
/// cmake --build build
///
/// This proves interoperability between the C++ MCP server (fastmcpp)
/// and the Copilot CLI, all orchestrated from a single executable.
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
// fastmcpp includes
#include "fastmcpp/app.hpp"
#include "fastmcpp/mcp/handler.hpp"
#include "fastmcpp/server/sse_server.hpp"
// Copilot SDK includes
#include <copilot/copilot.hpp>
using fastmcpp::FastMCP;
using fastmcpp::Json;
using fastmcpp::server::SseServerWrapper;
// =============================================================================
// Constants
// =============================================================================
constexpr int MCP_SERVER_PORT = 18080;
constexpr const char* MCP_SERVER_HOST = "127.0.0.1";
// =============================================================================
// MCP Server Setup (using fastmcpp)
// =============================================================================
/// Create and configure the fastmcpp MCP server
std::unique_ptr<SseServerWrapper> create_mcp_server()
{
// Create the FastMCP application
static FastMCP app("CopilotInteropTools", "1.0.0");
// Register tools that Copilot can use
// Tool 1: Secret vault lookup
app.tool(
"secret_vault_lookup",
Json{{"type", "object"},
{"properties",
Json{{"key", Json{{"type", "string"}, {"description", "The secret key to look up"}}}}},
{"required", Json::array({"key"})}},
[](const Json& args) -> Json
{
std::string key = args.value("key", std::string(""));
// Simulated secret vault
std::map<std::string, std::string> vault = {
{"API_TOKEN", "tok_live_abc123xyz"},
{"DATABASE_URL", "postgres://localhost:5432/mydb"},
{"ENCRYPTION_KEY", "aes256_secret_key_here"},
{"INTEROP_TEST", "FASTMCPP_COPILOT_SUCCESS"}};
auto it = vault.find(key);
if (it != vault.end())
{
return Json{
{"content",
Json::array(
{Json{{"type", "text"}, {"text", "Secret '" + key + "': " + it->second}}})}};
}
else
{
return Json{
{"content", Json::array({Json{{"type", "text"},
{"text", "No secret found for key: " + key}}})}};
}
},
FastMCP::ToolOptions{.description =
"Look up secrets from the vault. "
"Available keys: API_TOKEN, DATABASE_URL, ENCRYPTION_KEY, INTEROP_TEST"});
// Tool 2: Calculator with operation logging
app.tool(
"logged_calculator",
Json{{"type", "object"},
{"properties",
Json{{"operation", Json{{"type", "string"}, {"enum", Json::array({"add", "multiply"})}}},
{"a", Json{{"type", "number"}}},
{"b", Json{{"type", "number"}}}}},
{"required", Json::array({"operation", "a", "b"})}},
[](const Json& args) -> Json
{
std::string op = args.value("operation", std::string("add"));
double a = args.value("a", 0.0);
double b = args.value("b", 0.0);
double result;
std::string op_symbol;
if (op == "multiply")
{
result = a * b;
op_symbol = "*";
}
else
{
result = a + b;
op_symbol = "+";
}
std::cout << "[MCP Server] Calculator: " << a << " " << op_symbol << " " << b << " = "
<< result << "\n";
std::ostringstream oss;
oss << a << " " << op_symbol << " " << b << " = " << result;
return Json{
{"content", Json::array({Json{{"type", "text"}, {"text", oss.str()}}})}};
},
FastMCP::ToolOptions{.description = "Perform logged calculations (add or multiply)"});
// Tool 3: System info
app.tool(
"get_system_info",
Json{{"type", "object"}, {"properties", Json::object()}},
[](const Json&) -> Json
{
Json info = {{"server", "fastmcpp"},
{"version", "1.0.0"},
{"transport", "SSE"},
{"host", MCP_SERVER_HOST},
{"port", MCP_SERVER_PORT},
{"interop_status", "operational"}};
return Json{
{"content", Json::array({Json{{"type", "text"}, {"text", info.dump(2)}}})}};
},
FastMCP::ToolOptions{.description = "Get information about this MCP server"});
// Create the MCP handler
auto handler = fastmcpp::mcp::make_mcp_handler(app);
// Create and return the SSE server
return std::make_unique<SseServerWrapper>(handler, MCP_SERVER_HOST, MCP_SERVER_PORT);
}
// =============================================================================
// Main
// =============================================================================
int main()
{
try
{
std::cout << "=== Copilot SDK + fastmcpp In-Process Integration ===\n\n";
// ---------------------------------------------------------------------
// Step 1: Start the fastmcpp MCP server
// ---------------------------------------------------------------------
std::cout << "Starting fastmcpp MCP server on http://" << MCP_SERVER_HOST << ":"
<< MCP_SERVER_PORT << "/sse...\n";
auto mcp_server = create_mcp_server();
if (!mcp_server->start())
{
std::cerr << "Failed to start MCP server!\n";
return 1;
}
std::cout << "MCP server started successfully.\n";
std::cout << "Tools available: secret_vault_lookup, logged_calculator, get_system_info\n\n";
// Give the server a moment to fully initialize
std::this_thread::sleep_for(std::chrono::milliseconds(500));
// ---------------------------------------------------------------------
// Step 2: Create Copilot SDK client and configure MCP server
// ---------------------------------------------------------------------
std::cout << "Creating Copilot SDK client...\n";
copilot::ClientOptions client_opts;
client_opts.log_level = copilot::LogLevel::Info;
copilot::Client client(client_opts);
client.start().get();
std::cout << "Copilot client connected.\n\n";
// Configure the session to use our in-process MCP server
copilot::McpRemoteServerConfig mcp_config;
mcp_config.type = "sse";
mcp_config.url = std::string("http://") + MCP_SERVER_HOST + ":" +
std::to_string(MCP_SERVER_PORT) + "/sse";
mcp_config.tools = {"*"}; // Expose all tools from this server
std::map<std::string, copilot::json> mcp_servers;
mcp_servers["fastmcpp-tools"] = mcp_config;
copilot::SessionConfig session_config;
session_config.mcp_servers = mcp_servers;
auto session = client.create_session(session_config).get();
std::cout << "Session created: " << session->session_id() << "\n";
std::cout << "MCP server 'fastmcpp-tools' configured.\n\n";
// ---------------------------------------------------------------------
// Step 3: Interactive loop - use MCP tools via Copilot
// ---------------------------------------------------------------------
std::mutex mtx;
std::condition_variable cv;
std::atomic<bool> idle{false};
auto subscription = session->on(
[&](const copilot::SessionEvent& event)
{
if (auto* msg = event.try_as<copilot::AssistantMessageData>())
{
std::cout << "\nAssistant: " << msg->content << "\n";
}
else if (auto* tool_start = event.try_as<copilot::ToolExecutionStartData>())
{
std::cout << "\n[Copilot calling tool: " << tool_start->tool_name << "]\n";
if (tool_start->arguments)
std::cout << " Args: " << tool_start->arguments->dump() << "\n";
}
else if (auto* tool_complete = event.try_as<copilot::ToolExecutionCompleteData>())
{
std::cout << "[Tool execution complete]\n";
if (tool_complete->result)
std::cout << " Result: " << tool_complete->result->content << "\n";
}
else if (auto* error = event.try_as<copilot::SessionErrorData>())
{
std::cerr << "Error: " << error->message << "\n";
}
else if (event.type == copilot::SessionEventType::SessionIdle)
{
std::lock_guard<std::mutex> lock(mtx);
idle = true;
cv.notify_one();
}
}
);
std::cout << "=== Ready for interaction ===\n";
std::cout << "Try these prompts to test the MCP tools:\n";
std::cout << " 1. 'Look up the INTEROP_TEST secret'\n";
std::cout << " 2. 'Calculate 42 * 17 using the calculator'\n";
std::cout << " 3. 'Get system info from the MCP server'\n";
std::cout << "\nType 'quit' to exit.\n\n> ";
std::string line;
while (std::getline(std::cin, line))
{
if (line == "quit" || line == "exit")
break;
if (line.empty())
{
std::cout << "> ";
continue;
}
idle = false;
copilot::MessageOptions msg_opts;
msg_opts.prompt = line;
session->send(msg_opts).get();
{
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [&idle]() { return idle.load(); });
}
std::cout << "\n> ";
}
// ---------------------------------------------------------------------
// Cleanup
// ---------------------------------------------------------------------
std::cout << "\nCleaning up...\n";
session->destroy().get();
client.stop().get();
mcp_server->stop();
std::cout << "Done!\n";
return 0;
}
catch (const std::exception& e)
{
std::cerr << "Error: " << e.what() << "\n";
return 1;
}
}