-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_instructions.cpp
More file actions
300 lines (242 loc) · 11.5 KB
/
test_instructions.cpp
File metadata and controls
300 lines (242 loc) · 11.5 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
/// @file test_instructions.cpp
/// @brief E2E tests for the `instructions` field in MCP InitializeResult.
///
/// Covers all make_mcp_handler overloads and verifies instructions round-trips
/// through the client/server stack.
#include "fastmcpp/app.hpp"
#include "fastmcpp/client/client.hpp"
#include "fastmcpp/client/transports.hpp"
#include "fastmcpp/mcp/handler.hpp"
#include "fastmcpp/proxy.hpp"
#include <cassert>
#include <iostream>
#include <string>
using namespace fastmcpp;
static Json request(int id, const std::string& method, Json params = Json::object())
{
return Json{{"jsonrpc", "2.0"}, {"id", id}, {"method", method}, {"params", params}};
}
// ============================================================================
// 1. Bare handler (server_name, version, ToolManager) with instructions
// ============================================================================
static void test_bare_handler_with_instructions()
{
std::cout << "test_bare_handler_with_instructions...\n";
tools::ToolManager tm;
tools::Tool echo{"echo", Json{{"type", "object"}}, Json::object(),
[](const Json& in) { return in; }};
tm.register_tool(echo);
auto handler = mcp::make_mcp_handler("bare_srv", "1.0", tm, {}, {},
std::string("Bare handler instructions."));
auto resp = handler(request(1, "initialize"));
assert(resp.contains("result"));
assert(resp["result"].contains("instructions"));
assert(resp["result"]["instructions"] == "Bare handler instructions.");
std::cout << " PASS\n";
}
// ============================================================================
// 2. Bare handler without instructions (should be absent)
// ============================================================================
static void test_bare_handler_no_instructions()
{
std::cout << "test_bare_handler_no_instructions...\n";
tools::ToolManager tm;
tools::Tool echo{"echo", Json{{"type", "object"}}, Json::object(),
[](const Json& in) { return in; }};
tm.register_tool(echo);
auto handler = mcp::make_mcp_handler("bare_srv", "1.0", tm);
auto resp = handler(request(1, "initialize"));
assert(resp.contains("result"));
assert(!resp["result"].contains("instructions"));
std::cout << " PASS\n";
}
// ============================================================================
// 3. Server + tools_meta overload
// ============================================================================
static void test_server_tools_meta_instructions()
{
std::cout << "test_server_tools_meta_instructions...\n";
server::Server srv("meta_srv", "1.0", std::nullopt, std::nullopt,
std::string("Server tools_meta instructions."));
std::vector<std::tuple<std::string, std::string, Json>> tools_meta = {
{"echo", "Echo tool", Json{{"type", "object"}}}};
auto handler = mcp::make_mcp_handler("meta_srv", "1.0", srv, tools_meta);
auto resp = handler(request(1, "initialize"));
assert(resp["result"].contains("instructions"));
assert(resp["result"]["instructions"] == "Server tools_meta instructions.");
std::cout << " PASS\n";
}
// ============================================================================
// 4. Server + ToolManager overload
// ============================================================================
static void test_server_toolmanager_instructions()
{
std::cout << "test_server_toolmanager_instructions...\n";
server::Server srv("stm_srv", "1.0", std::nullopt, std::nullopt,
std::string("Server+TM instructions."));
tools::ToolManager tm;
tools::Tool echo{"echo", Json{{"type", "object"}}, Json::object(),
[](const Json& in) { return in; }};
tm.register_tool(echo);
auto handler = mcp::make_mcp_handler("stm_srv", "1.0", srv, tm);
auto resp = handler(request(1, "initialize"));
assert(resp["result"].contains("instructions"));
assert(resp["result"]["instructions"] == "Server+TM instructions.");
std::cout << " PASS\n";
}
// ============================================================================
// 5. Server + ToolManager + ResourceManager + PromptManager overload
// ============================================================================
static void test_server_full_instructions()
{
std::cout << "test_server_full_instructions...\n";
server::Server srv("full_srv", "1.0", std::nullopt, std::nullopt,
std::string("Full server instructions."));
tools::ToolManager tm;
resources::ResourceManager rm;
prompts::PromptManager pm;
auto handler = mcp::make_mcp_handler("full_srv", "1.0", srv, tm, rm, pm);
auto resp = handler(request(1, "initialize"));
assert(resp["result"].contains("instructions"));
assert(resp["result"]["instructions"] == "Full server instructions.");
std::cout << " PASS\n";
}
// ============================================================================
// 6. FastMCP handler (with client round-trip via InProcessMcpTransport)
// ============================================================================
static void test_fastmcp_instructions_e2e()
{
std::cout << "test_fastmcp_instructions_e2e...\n";
FastMCP app("e2e_srv", "1.0.0", std::nullopt, std::nullopt,
std::string("End-to-end instructions."));
app.tool("noop", [](const Json&) { return Json{{"ok", true}}; });
auto handler = mcp::make_mcp_handler(app);
// Verify via raw handler
auto resp = handler(request(1, "initialize"));
assert(resp["result"].contains("instructions"));
assert(resp["result"]["instructions"] == "End-to-end instructions.");
// Verify via client (full round-trip)
client::Client c(std::make_unique<client::InProcessMcpTransport>(handler));
auto init_result = c.call(
"initialize", Json{{"protocolVersion", "2024-11-05"},
{"capabilities", Json::object()},
{"clientInfo", Json{{"name", "e2e-test"}, {"version", "1.0.0"}}}});
assert(init_result.contains("instructions"));
assert(init_result["instructions"] == "End-to-end instructions.");
std::cout << " PASS\n";
}
// ============================================================================
// 7. FastMCP with set_instructions (mutation)
// ============================================================================
static void test_fastmcp_set_instructions()
{
std::cout << "test_fastmcp_set_instructions...\n";
FastMCP app("setter_srv", "1.0.0");
assert(!app.instructions().has_value());
app.set_instructions("Mutated instructions.");
assert(app.instructions().has_value());
assert(*app.instructions() == "Mutated instructions.");
auto handler = mcp::make_mcp_handler(app);
auto resp = handler(request(1, "initialize"));
assert(resp["result"].contains("instructions"));
assert(resp["result"]["instructions"] == "Mutated instructions.");
// Clear and verify absent
app.set_instructions(std::nullopt);
auto handler2 = mcp::make_mcp_handler(app);
auto resp2 = handler2(request(2, "initialize"));
assert(!resp2["result"].contains("instructions"));
std::cout << " PASS\n";
}
// ============================================================================
// 8. ProxyApp handler
// ============================================================================
static void test_proxy_instructions()
{
std::cout << "test_proxy_instructions...\n";
// Create a backend FastMCP server
FastMCP backend("backend", "1.0.0");
backend.tool("ping", [](const Json&) { return Json{{"pong", true}}; });
auto backend_handler = mcp::make_mcp_handler(backend);
// Create proxy with instructions
auto client_factory = [backend_handler]()
{ return client::Client(std::make_unique<client::InProcessMcpTransport>(backend_handler)); };
ProxyApp proxy(client_factory, "proxy_srv", "1.0.0", std::string("Proxy instructions."));
auto handler = mcp::make_mcp_handler(proxy);
auto resp = handler(request(1, "initialize"));
assert(resp["result"].contains("instructions"));
assert(resp["result"]["instructions"] == "Proxy instructions.");
std::cout << " PASS\n";
}
// ============================================================================
// 9. ProxyApp with set_instructions
// ============================================================================
static void test_proxy_set_instructions()
{
std::cout << "test_proxy_set_instructions...\n";
FastMCP backend("backend2", "1.0.0");
backend.tool("echo", [](const Json& in) { return in; });
auto backend_handler = mcp::make_mcp_handler(backend);
auto client_factory = [backend_handler]()
{ return client::Client(std::make_unique<client::InProcessMcpTransport>(backend_handler)); };
ProxyApp proxy(client_factory, "proxy_set", "1.0.0");
assert(!proxy.instructions().has_value());
proxy.set_instructions("Proxy updated.");
auto handler = mcp::make_mcp_handler(proxy);
auto resp = handler(request(1, "initialize"));
assert(resp["result"].contains("instructions"));
assert(resp["result"]["instructions"] == "Proxy updated.");
std::cout << " PASS\n";
}
// ============================================================================
// 10. Server accessors
// ============================================================================
static void test_server_accessors()
{
std::cout << "test_server_accessors...\n";
server::Server srv("acc_srv", "1.0", std::nullopt, std::nullopt, std::string("Initial."));
assert(srv.instructions().has_value());
assert(*srv.instructions() == "Initial.");
srv.set_instructions("Changed.");
assert(*srv.instructions() == "Changed.");
srv.set_instructions(std::nullopt);
assert(!srv.instructions().has_value());
std::cout << " PASS\n";
}
// ============================================================================
// 11. Backward-compatible constructor parameter order
// ============================================================================
static void test_legacy_constructor_compatibility()
{
std::cout << "test_legacy_constructor_compatibility...\n";
std::vector<std::shared_ptr<providers::Provider>> providers;
FastMCP app_with_vector("legacy_app_vector", "1.0.0", std::nullopt, std::nullopt, providers, 3,
false);
assert(!app_with_vector.instructions().has_value());
assert(app_with_vector.list_page_size() == 3);
assert(!app_with_vector.dereference_schemas());
FastMCP app_with_braces("legacy_app_braces", "1.0.0", std::nullopt, std::nullopt, {}, 0, false);
assert(!app_with_braces.instructions().has_value());
assert(app_with_braces.list_page_size() == 0);
assert(!app_with_braces.dereference_schemas());
server::Server legacy_srv("legacy_srv", "1.0.0", std::nullopt, std::nullopt, true);
assert(!legacy_srv.instructions().has_value());
assert(legacy_srv.strict_input_validation().has_value());
assert(*legacy_srv.strict_input_validation());
std::cout << " PASS\n";
}
int main()
{
test_bare_handler_with_instructions();
test_bare_handler_no_instructions();
test_server_tools_meta_instructions();
test_server_toolmanager_instructions();
test_server_full_instructions();
test_fastmcp_instructions_e2e();
test_fastmcp_set_instructions();
test_proxy_instructions();
test_proxy_set_instructions();
test_server_accessors();
test_legacy_constructor_compatibility();
std::cout << "All instructions tests passed\n";
return 0;
}