Skip to content

Commit 5dfd91e

Browse files
Update examples
V2.38
1 parent b04272f commit 5dfd91e

9 files changed

Lines changed: 267 additions & 18 deletions

File tree

examples/func_registry/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Function Registry Demo
2+
3+
This example demonstrates the `func_registry` library: a type-safe C++ function registration and invocation system.
4+
5+
The registry allows you to register functions with metadata and call them back by name, while the compiler enforces type safety.
6+
7+
## Build
8+
9+
```powershell
10+
cmake -B build -S .
11+
cmake --build build --target func_registry_demo
12+
```
13+
14+
## Run
15+
16+
```powershell
17+
.\build\func_registry_demo.exe
18+
```
19+
20+
Linux/macOS:
21+
22+
```bash
23+
./build/func_registry_demo
24+
```
25+
26+
## Features Demonstrated
27+
28+
- Register static functions, member functions, and lambdas with descriptions
29+
- Call functions by name with type-safe return values
30+
- Support for optional parameters with defaults
31+
- Print registered function signatures

examples/json_invoke/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# JSON Invoke Demo
2+
3+
This example demonstrates the `json_invoke` library: a JSON-based function invocation system that bridges C++ functions with LLM-style tool calling.
4+
5+
Functions are registered and invoked via JSON payloads, supporting:
6+
- Standard JSON request format
7+
- LLM-compatible tool call format (matching Claude, ChatGPT conventions)
8+
- Complex types (structs, arrays, nested objects)
9+
- Custom type serialization
10+
11+
## Build
12+
13+
```powershell
14+
cmake -B build -S .
15+
cmake --build build --target json_invoke_demo
16+
```
17+
18+
## Run
19+
20+
```powershell
21+
.\build\json_invoke_demo.exe
22+
```
23+
24+
Linux/macOS:
25+
26+
```bash
27+
./build/json_invoke_demo
28+
```
29+
30+
## Features Demonstrated
31+
32+
- Invoke functions by JSON request with typed parameters
33+
- Support for LLM tool-call request format with JSON-encoded arguments
34+
- Serialize and deserialize complex C++ types to/from JSON
35+
- Invoke functions and get results back as C++ types or JSON
36+
- Process nested objects and arrays in function arguments

examples/json_stateful/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# JSON Stateful Demo
2+
3+
This example demonstrates the `json_session_invoke` library: a stateful object lifecycle system for JSON invocation.
4+
5+
Functions can operate in two modes:
6+
- **Stateless**: Invoke and return immediately
7+
- **Stateful**: Create a persistent object handle, call methods on it, then destroy it
8+
9+
The lifecycle of a stateful object is: `create -> method calls -> destroy`.
10+
11+
## Build
12+
13+
```powershell
14+
cmake -B build -S .
15+
cmake --build build --target json_stateful_demo
16+
```
17+
18+
## Run
19+
20+
```powershell
21+
.\build\json_stateful_demo.exe
22+
```
23+
24+
Linux/macOS:
25+
26+
```bash
27+
./build/json_stateful_demo
28+
```
29+
30+
## Features Demonstrated
31+
32+
- Register stateless functions that invoke immediately
33+
- Register stateful object types with create/method/destroy lifecycle
34+
- Serialize stateful handles for transport over HTTP or IPC
35+
- Mix stateless and stateful operations in the same adapter
36+
- Thread-safe object lifecycle management

examples/json_tracing/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# JSON Tracing Demo
2+
3+
This example demonstrates function call tracing and detailed diagnostic output.
4+
5+
When a trace sink is installed, the adapter emits detailed events for:
6+
- Function invocation with arguments
7+
- Execution time
8+
- Success and failure outcomes
9+
- Parameter serialization and deserialization
10+
11+
Tracing is useful for debugging, performance analysis, and audit logging.
12+
13+
## Build
14+
15+
```powershell
16+
cmake -B build -S .
17+
cmake --build build --target json_tracing_demo
18+
```
19+
20+
## Run
21+
22+
```powershell
23+
.\build\json_tracing_demo.exe
24+
```
25+
26+
Linux/macOS:
27+
28+
```bash
29+
./build/json_tracing_demo
30+
```
31+
32+
## Features Demonstrated
33+
34+
- Install a trace sink to observe function invocations
35+
- Capture success and error traces for debugging
36+
- Serialize trace events to JSON for analysis
37+
- Understand call parameters and results through trace output

examples/mcp_http/mcp_http_client_demo.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,21 +155,24 @@ def run_demo(client: McpHttpClient) -> None:
155155
)
156156
print("write_file:", extract_text_result(write_response))
157157

158-
open_response = client.call_tool("create_text_file", {"path": "build/_http_demo_scratch.txt"})
159-
handle = json.loads(extract_text_result(open_response))
160-
print("create_text_file handle:", json.dumps(handle))
161-
162-
lines_response = client.call_tool("text_file_lines", {"handle": handle})
163-
lines = json.loads(extract_text_result(lines_response))
164-
print("text_file_lines:", lines)
165-
166-
client.call_tool("text_file_replace_line", {"handle": handle, "line_number": 2, "content": "BETA (edited)"})
167-
client.call_tool("text_file_append", {"handle": handle, "content": "delta (appended)"})
168-
169-
save_response = client.call_tool("text_file_save", {"handle": handle})
170-
print("text_file_save:", extract_text_result(save_response))
171-
172-
client.call_tool("destroy_text_file", {"handle": handle})
158+
handle = None
159+
try:
160+
open_response = client.call_tool("create_text_file", {"path": "build/_http_demo_scratch.txt"})
161+
handle = json.loads(extract_text_result(open_response))
162+
print("create_text_file handle:", json.dumps(handle))
163+
164+
lines_response = client.call_tool("text_file_lines", {"handle": handle})
165+
lines = json.loads(extract_text_result(lines_response))
166+
print("text_file_lines:", lines)
167+
168+
client.call_tool("text_file_replace_line", {"handle": handle, "line_number": 2, "content": "BETA (edited)"})
169+
client.call_tool("text_file_append", {"handle": handle, "content": "delta (appended)"})
170+
171+
save_response = client.call_tool("text_file_save", {"handle": handle})
172+
print("text_file_save:", extract_text_result(save_response))
173+
finally:
174+
if handle is not None:
175+
client.call_tool("destroy_text_file", {"handle": handle})
173176

174177
# Verify the edit landed on disk
175178
read_response = client.call_tool("read_file", {"path": "build/_http_demo_scratch.txt"})

examples/mcp_http/mcp_http_gateway_demo.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,11 +660,21 @@ int main(int argc, char** argv)
660660
try
661661
{
662662
std::string host = "127.0.0.1";
663-
int port = 8080;
663+
int port = 18084;
664664
std::filesystem::path backend_executable;
665665
for (int index = 1; index < argc; ++index)
666666
{
667667
const std::string arg = argv[index];
668+
if (arg == "--help" || arg == "-h")
669+
{
670+
std::cerr << "Usage: " << (argc > 0 ? argv[0] : "mcp_http_gateway_demo") << " [options]\n\n"
671+
<< "Options:\n"
672+
<< " --host <address> HTTP listen address (default: 127.0.0.1)\n"
673+
<< " --port <port> HTTP listen port (default: 18084, valid: 1-65535)\n"
674+
<< " --backend-executable <path> Path to stdio MCP backend (required if auto-detect fails)\n"
675+
<< " --help, -h Show this help message\n";
676+
return 0;
677+
}
668678
if (arg == "--host" && index + 1 < argc)
669679
{
670680
host = argv[++index];
@@ -673,7 +683,16 @@ int main(int argc, char** argv)
673683

674684
if (arg == "--port" && index + 1 < argc)
675685
{
676-
port = parsePort(argv[++index]);
686+
try {
687+
port = parsePort(argv[++index]);
688+
if (port < 1 || port > 65535)
689+
{
690+
throw std::runtime_error("port must be between 1 and 65535");
691+
}
692+
} catch (const std::exception& e) {
693+
std::cerr << "Invalid port: " << e.what() << std::endl;
694+
return 1;
695+
}
677696
continue;
678697
}
679698

@@ -682,6 +701,10 @@ int main(int argc, char** argv)
682701
backend_executable = argv[++index];
683702
continue;
684703
}
704+
705+
std::cerr << "unknown option: " << arg << std::endl;
706+
std::cerr << "use --help for usage information." << std::endl;
707+
return 1;
685708
}
686709

687710
const std::filesystem::path resolved_backend = resolveBackendExecutable(backend_executable, argc > 0 ? argv[0] : "");

examples/mcp_http/run_deepseek_mcp_http_chat.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,13 @@ def call_mcp_tool(client: McpHttpClient, tool_call: dict[str, Any]) -> str:
8989
function = tool_call.get("function", {})
9090
tool_name = function.get("name", "")
9191
arguments_text = function.get("arguments", "")
92-
arguments = json.loads(arguments_text) if arguments_text else {}
92+
try:
93+
arguments = json.loads(arguments_text) if arguments_text else {}
94+
except json.JSONDecodeError as exc:
95+
raise RuntimeError(
96+
f"Failed to parse arguments for tool '{tool_name}': {exc}. "
97+
f"Raw arguments text: {repr(arguments_text)}"
98+
) from exc
9399
response = client.call_tool(tool_name, arguments)
94100
return extract_text_result(response)
95101

examples/task_scheduler/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Task Scheduler Demo
2+
3+
This example demonstrates the `task_scheduler` library: a priority-based task execution scheduler.
4+
5+
The scheduler manages concurrent execution of function invocations with different priority levels:
6+
- **FreeReadOnly**: Read-only operations that can run in parallel
7+
- **ObjectExclusive**: Operations that exclusively lock one object
8+
- **FactoryLane**: Object creation operations
9+
- **ToolExclusive**: Exclusive tool access
10+
- **SessionBarrier**: Session-level synchronization points
11+
12+
This pattern is useful for MCP servers handling concurrent client requests.
13+
14+
## Build
15+
16+
```powershell
17+
cmake -B build -S .
18+
cmake --build build --target task_scheduler_demo
19+
```
20+
21+
## Run
22+
23+
```powershell
24+
.\build\task_scheduler_demo.exe
25+
```
26+
27+
Linux/macOS:
28+
29+
```bash
30+
./build/task_scheduler_demo
31+
```
32+
33+
## Features Demonstrated
34+
35+
- Submit requests with different scheduling categories
36+
- Observe concurrent execution of read-only operations
37+
- Ensure exclusive access for object mutation
38+
- Track scheduling timing and thread assignment
39+
- Use futures to wait for results across threads

examples/trace_recorder/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Trace Recorder Demo
2+
3+
This example demonstrates the `trace_recorder` library: an in-memory recording system for function call traces.
4+
5+
Instead of streaming traces to a sink, the `VectorTraceRecorder` buffers all trace events and can export them as a JSON array for inspection or analysis.
6+
7+
This is useful for:
8+
- Logging complete execution traces for debugging
9+
- Collecting performance metrics
10+
- Auditing function calls for compliance
11+
- Integration with external tracing systems
12+
13+
## Build
14+
15+
```powershell
16+
cmake -B build -S .
17+
cmake --build build --target trace_recorder_demo
18+
```
19+
20+
## Run
21+
22+
```powershell
23+
.\build\trace_recorder_demo.exe
24+
```
25+
26+
Linux/macOS:
27+
28+
```bash
29+
./build/trace_recorder_demo
30+
```
31+
32+
## Features Demonstrated
33+
34+
- Record all function invocations and results
35+
- Export recorded traces as structured JSON
36+
- Clear trace buffer between operations
37+
- Inspect both successful and failed function calls
38+
- Analyze serialization and performance timing

0 commit comments

Comments
 (0)