forked from 0xeb/copilot-sdk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_servers.cpp
More file actions
194 lines (162 loc) · 6.91 KB
/
Copy pathmcp_servers.cpp
File metadata and controls
194 lines (162 loc) · 6.91 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
// Copyright (c) 2025 Elias Bachaalany
// SPDX-License-Identifier: MIT
/// @file mcp_servers.cpp
/// @brief Example demonstrating MCP server configuration
///
/// This example shows how to:
/// 1. Configure local (stdio) MCP servers
/// 2. Configure remote (HTTP/SSE) MCP servers
/// 3. Use multiple MCP servers in a single session
/// 4. Restrict which tools are exposed from MCP servers
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <copilot/copilot.hpp>
#include <iostream>
#include <mutex>
#include <string>
int main()
{
try
{
copilot::ClientOptions options;
options.log_level = copilot::LogLevel::Info;
copilot::Client client(options);
std::cout << "=== MCP Servers Example ===\n\n";
std::cout << "This example demonstrates configuring MCP (Model Context Protocol) servers.\n";
std::cout << "MCP servers extend Copilot's capabilities with external tools.\n\n";
client.start().get();
// ---------------------------------------------------------------------
// Configure MCP Servers
// ---------------------------------------------------------------------
std::map<std::string, copilot::json> mcp_servers;
// Example 1: Local filesystem MCP server (stdio-based)
// This server provides file system tools
copilot::McpLocalServerConfig filesystem_server;
filesystem_server.type = "local";
filesystem_server.command = "npx";
filesystem_server.args = {"-y", "@anthropic/mcp-server-filesystem", "/tmp"};
filesystem_server.tools = {"*"}; // Expose all tools from this server
filesystem_server.timeout = 30000; // 30 second timeout
mcp_servers["filesystem"] = filesystem_server;
// Example 2: Local MCP server with environment variables
copilot::McpLocalServerConfig custom_server;
custom_server.type = "local";
custom_server.command = "python";
custom_server.args = {"-m", "my_mcp_server"};
custom_server.tools = {"my_tool_1", "my_tool_2"}; // Only expose specific tools
custom_server.env = std::map<std::string, std::string>{
{"API_KEY", "your-api-key"},
{"DEBUG", "true"}
};
custom_server.cwd = "/path/to/server";
// Uncomment to add this server:
// mcp_servers["custom"] = custom_server;
// Example 3: Remote HTTP MCP server
copilot::McpRemoteServerConfig remote_server;
remote_server.type = "http";
remote_server.url = "https://api.example.com/mcp";
remote_server.tools = {"*"};
remote_server.timeout = 60000; // 60 second timeout
remote_server.headers = std::map<std::string, std::string>{
{"Authorization", "Bearer your-token"},
{"X-Custom-Header", "value"}
};
// Uncomment to add this server:
// mcp_servers["remote-api"] = remote_server;
// Example 4: SSE (Server-Sent Events) MCP server
copilot::McpRemoteServerConfig sse_server;
sse_server.type = "sse";
sse_server.url = "https://api.example.com/mcp/sse";
sse_server.tools = {"stream_tool"};
// Uncomment to add this server:
// mcp_servers["sse-server"] = sse_server;
// ---------------------------------------------------------------------
// Create Session with MCP Servers
// ---------------------------------------------------------------------
copilot::SessionConfig config;
config.mcp_servers = mcp_servers;
std::cout << "Configured MCP servers:\n";
for (const auto& [name, _] : mcp_servers)
std::cout << " - " << name << "\n";
std::cout << "\n";
std::cout << "Note: This example configures a filesystem MCP server.\n";
std::cout << "Make sure you have the MCP server installed:\n";
std::cout << " npm install -g @anthropic/mcp-server-filesystem\n\n";
auto session = client.create_session(config).get();
std::cout << "Session created: " << session->session_id() << "\n\n";
// Synchronization
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[MCP Tool: " << tool_start->tool_name << "]\n";
if (tool_start->arguments)
std::cout << " Args: " << tool_start->arguments->dump(2) << "\n";
}
else if (auto* tool_complete = event.try_as<copilot::ToolExecutionCompleteData>())
{
std::cout << "[Tool Complete]\n";
if (tool_complete->result)
std::cout << " Result: " << tool_complete->result->content.substr(0, 200)
<< (tool_complete->result->content.length() > 200 ? "..." : "")
<< "\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();
}
}
);
// Interactive loop
std::cout << "Try commands that use MCP server tools.\n";
std::cout << "With filesystem server, try:\n";
std::cout << " - 'List files in /tmp'\n";
std::cout << " - 'Read the contents of /tmp/test.txt'\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();
return 0;
}
catch (const std::exception& e)
{
std::cerr << "Error: " << e.what() << "\n";
return 1;
}
}