-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcontext_introspection.cpp
More file actions
194 lines (162 loc) · 7.04 KB
/
context_introspection.cpp
File metadata and controls
194 lines (162 loc) · 7.04 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
// Example demonstrating Context API for resource and prompt introspection (v2.13.0+)
//
// This example shows how tools can use the Context class to discover and access
// available resources and prompts at runtime. This mirrors the Python fastmcp
// Context API for introspection capabilities.
#include "fastmcpp/prompts/manager.hpp"
#include "fastmcpp/resources/manager.hpp"
#include "fastmcpp/server/context.hpp"
#include "fastmcpp/tools/manager.hpp"
#include "fastmcpp/tools/tool.hpp"
#include <iomanip>
#include <iostream>
int main()
{
using namespace fastmcpp;
using Json = nlohmann::json;
std::cout << "=== Context Introspection Example (v2.13.0+) ===\n\n";
// ============================================================================
// Step 1: Set up Resources
// ============================================================================
resources::ResourceManager resource_mgr;
// Register some sample resources
resources::Resource doc1;
doc1.uri = "file://docs/readme.txt";
doc1.name = "readme.txt";
doc1.id = Id{"file://docs/readme.txt"};
doc1.kind = resources::Kind::File;
doc1.metadata = Json{{"description", "Project README"}, {"size", 1024}};
resource_mgr.register_resource(doc1);
resources::Resource doc2;
doc2.uri = "file://docs/api.txt";
doc2.name = "api.txt";
doc2.id = Id{"file://docs/api.txt"};
doc2.kind = resources::Kind::File;
doc2.metadata = Json{{"description", "API Documentation"}, {"size", 2048}};
resource_mgr.register_resource(doc2);
resources::Resource config;
config.uri = "config://app.json";
config.name = "app.json";
config.id = Id{"config://app.json"};
config.kind = resources::Kind::Json;
config.metadata = Json{{"description", "Application config"}};
resource_mgr.register_resource(config);
// ============================================================================
// Step 2: Set up Prompts
// ============================================================================
prompts::PromptManager prompt_mgr;
// Register some sample prompts with template variables
prompts::Prompt greeting("Hello {{name}}, welcome to {{app}}!");
prompt_mgr.add("greeting", greeting);
prompts::Prompt summary("Summarize {{topic}} in {{length}} words.");
prompt_mgr.add("summary_prompt", summary);
// ============================================================================
// Step 3: Create Context and demonstrate introspection
// ============================================================================
server::Context ctx(resource_mgr, prompt_mgr);
std::cout << "1. Listing Resources:\n";
std::cout << " " << std::string(40, '-') << "\n";
auto resources = ctx.list_resources();
for (const auto& res : resources)
{
std::cout << " - URI: " << res.id.value << "\n";
std::cout << " Kind: " << resources::to_string(res.kind) << "\n";
std::cout << " Metadata: " << res.metadata.dump() << "\n\n";
}
std::cout << "\n2. Listing Prompts:\n";
std::cout << " " << std::string(40, '-') << "\n";
auto prompts = ctx.list_prompts();
for (const auto& prompt : prompts)
{
std::cout << " - Name: " << prompt.name << "\n";
std::cout << " Template: " << prompt.template_string() << "\n\n";
}
std::cout << "\n3. Getting and Rendering Prompts:\n";
std::cout << " " << std::string(40, '-') << "\n";
try
{
Json args = {{"name", "Alice"}, {"app", "FastMCP"}};
std::string rendered = ctx.get_prompt("greeting", args);
std::cout << " Rendered greeting: " << rendered << "\n\n";
args = {{"topic", "machine learning"}, {"length", "50"}};
rendered = ctx.get_prompt("summary_prompt", args);
std::cout << " Rendered summary: " << rendered << "\n\n";
}
catch (const std::exception& e)
{
std::cerr << " Error: " << e.what() << "\n\n";
}
std::cout << "\n4. Reading Resources:\n";
std::cout << " " << std::string(40, '-') << "\n";
try
{
std::string content = ctx.read_resource("file://docs/readme.txt");
std::cout << " " << content << "\n\n";
}
catch (const std::exception& e)
{
std::cerr << " Error: " << e.what() << "\n\n";
}
// ============================================================================
// Step 4: Demonstrate Context usage in a Tool
// ============================================================================
std::cout << "\n5. Using Context in a Tool:\n";
std::cout << " " << std::string(40, '-') << "\n";
tools::ToolManager tool_mgr;
// Define a tool that uses Context for introspection
tools::Tool analyze_resources{
"analyze_resources",
Json{{"type", "object"},
{"properties",
Json{{"filter_kind",
Json{{"type", "string"}, {"enum", Json::array({"file", "json", "text"})}}}}}},
Json{{"type", "object"}}, [&resource_mgr, &prompt_mgr](const Json& input) -> Json
{
// Create context for introspection
server::Context ctx(resource_mgr, prompt_mgr);
// List all resources
auto all_resources = ctx.list_resources();
// Filter by kind if specified
std::string filter = input.value("filter_kind", std::string(""));
int count = 0;
Json results = Json::array();
for (const auto& res : all_resources)
{
std::string kind_str = resources::to_string(res.kind);
if (filter.empty() || kind_str == filter)
{
results.push_back(Json{
{"uri", res.id.value}, {"kind", kind_str}, {"metadata", res.metadata}});
count++;
}
}
return Json{
{"content", Json::array({Json{
{"type", "text"},
{"text", std::string("Found ") +
std::to_string(count) + " resources"},
},
Json{{"type", "text"}, {"text", results.dump(2)}}})}};
}};
tool_mgr.register_tool(analyze_resources);
// Invoke the tool
Json tool_input = {{"filter_kind", "file"}};
std::cout << " Invoking tool with input: " << tool_input.dump() << "\n";
try
{
Json result = tool_mgr.invoke("analyze_resources", tool_input);
std::cout << " Tool result:\n";
if (result.contains("content") && result["content"].is_array())
{
for (const auto& item : result["content"])
if (item.contains("text"))
std::cout << " " << item["text"].get<std::string>() << "\n";
}
}
catch (const std::exception& e)
{
std::cerr << " Error: " << e.what() << "\n";
}
std::cout << "\n=== Example Complete ===\n";
return 0;
}