Repository navigation
Expand file tree
/
Copy pathmcp.cpp
More file actions
858 lines (661 loc) · 21.9 KB
/
Copy pathmcp.cpp
File metadata and controls
858 lines (661 loc) · 21.9 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
// Copyright (c) 2026 Vector 35 Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#include "mcp.h"
#include "mcpserver.h"
#include "rapidjsonwrapper.h"
#include <exception>
#include <stdexcept>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
using namespace BinaryNinja;
using namespace BinaryNinja::MCP;
namespace {
Ref<Logger> GetMcpLogger()
{
static Ref<Logger> logger = LogRegistry::CreateLogger("MCP");
return logger;
}
std::string TakeString(char* text)
{
std::string result = text ? text : "";
BNFreeString(text);
return result;
}
ArgumentResult<uint64_t> ParseWith(bool (*parse)(BNMcpToolCall*, const char*, uint64_t*, uint64_t, char**),
BNMcpToolCall* call, const rapidjson::Value& value, uint64_t here)
{
std::string json = detail::SerializeJson(value);
uint64_t result = 0;
char* error = nullptr;
if (!parse(call, json.c_str(), &result, here, &error))
return bn::base::unexpected(TakeString(error));
return result;
}
void InvokeHandler(void* ctxt, BNMcpToolCall* callHandle, const char* arguments, BNMcpToolResult* result)
{
auto& handler = *static_cast<ToolHandler*>(ctxt);
Ref<ToolCall> call = new ToolCall(BNNewMcpToolCallReference(callHandle));
rapidjson::Document document;
try
{
document.Parse(arguments);
}
catch (const ParseException& e)
{
ToolResult::Error("invalid_params", fmt::format("Arguments are not valid JSON: {}", e.what())).ApplyTo(result);
return;
}
std::optional<ToolResult> toolResult;
try
{
toolResult = handler(*call, document);
}
catch (const std::exception& e)
{
GetMcpLogger()->LogErrorForExceptionF(e, "MCP tool failed: {}", e.what());
toolResult = ToolResult::Error("internal_error", e.what());
}
catch (...)
{
GetMcpLogger()->LogError("MCP tool failed with an unknown exception");
toolResult = ToolResult::Error("internal_error", "The tool failed with an unknown exception");
}
toolResult->ApplyTo(result);
}
Ref<Tool> CreateToolUsing(const ToolDefinition& definition, const std::string& inputSchema, ToolHandler handler,
BNMcpTool* (*create)(const BNMcpToolDefinition*, const BNMcpToolCallbacks*))
{
BNMcpToolDefinition apiDefinition = {};
apiDefinition.name = definition.name.c_str();
apiDefinition.title = definition.title.c_str();
apiDefinition.description = definition.description.c_str();
apiDefinition.inputSchema = inputSchema.c_str();
apiDefinition.outputSchema = definition.outputSchema.empty() ? nullptr : definition.outputSchema.c_str();
apiDefinition.scope = static_cast<BNMcpToolScope>(definition.scope);
apiDefinition.annotations = definition.annotations;
auto* context = new ToolHandler(std::move(handler));
BNMcpToolCallbacks callbacks = {};
callbacks.context = context;
callbacks.invoke = InvokeHandler;
callbacks.freeObject = [](void* ctxt) { delete static_cast<ToolHandler*>(ctxt); };
BNMcpTool* tool = create(&apiDefinition, &callbacks);
if (!tool)
{
delete context;
return nullptr;
}
return new Tool(tool);
}
} // namespace
ToolCall::ToolCall(BNMcpToolCall* call)
{
m_object = call;
}
Ref<BinaryView> ToolCall::GetBinaryView() const
{
BNBinaryView* view = BNGetMcpToolCallBinaryView(m_object);
return view ? new BinaryView(view) : nullptr;
}
bool ToolCall::IsCancelled() const
{
return BNIsMcpToolCallCancelled(m_object);
}
void ToolCall::ReportProgress(double progress, double total, const std::string& message) const
{
BNReportMcpToolCallProgress(m_object, progress, total, message.c_str());
}
ArgumentResult<uint64_t> ToolCall::ParseAddress(const rapidjson::Value& value, uint64_t here) const
{
return ParseWith(BNParseMcpToolCallAddress, m_object, value, here);
}
ArgumentResult<uint64_t> ToolCall::ParseInteger(const rapidjson::Value& value, uint64_t here) const
{
return ParseWith(BNParseMcpToolCallInteger, m_object, value, here);
}
ToolResult ToolResult::Text(std::string text)
{
ToolResult result;
result.m_text.push_back(std::move(text));
return result;
}
ToolResult ToolResult::Structured(const rapidjson::Value& content)
{
ToolResult result;
result.m_structuredContent = detail::SerializeJson(content);
return result;
}
ToolResult ToolResult::Error(std::string code, std::string message, const rapidjson::Value* details)
{
ToolResult result;
result.m_error = ErrorInfo {std::move(code), std::move(message),
details ? std::optional(detail::SerializeJson(*details)) : std::nullopt};
return result;
}
ToolResult& ToolResult::AddText(std::string text)
{
m_text.push_back(std::move(text));
return *this;
}
ToolResult& ToolResult::AddWarning(std::string code, std::string message)
{
m_warnings.emplace_back(std::move(code), std::move(message));
return *this;
}
void ToolResult::ApplyTo(BNMcpToolResult* result) const
{
if (m_error)
{
BNSetMcpToolResultError(result, m_error->code.c_str(), m_error->message.c_str(),
m_error->details ? m_error->details->c_str() : nullptr);
}
else if (m_structuredContent && !BNSetMcpToolResultStructuredContent(result, m_structuredContent->c_str()))
{
BNSetMcpToolResultError(
result, "internal_error", "The tool produced structured content that is not a JSON object", nullptr);
return;
}
for (const auto& text : m_text)
BNAddMcpToolResultText(result, text.c_str());
for (const auto& [code, message] : m_warnings)
BNAddMcpToolResultWarning(result, code.c_str(), message.c_str());
}
std::string ToolResult::ToJson() const
{
BNMcpToolResult* result = BNCreateMcpToolResult();
ApplyTo(result);
std::string json = TakeString(BNGetMcpToolResultJson(result));
BNFreeMcpToolResult(result);
return json;
}
Tool::Tool(BNMcpTool* tool)
{
m_object = tool;
}
std::vector<Ref<Tool>> Tool::GetList()
{
size_t count = 0;
BNMcpTool** tools = BNGetMcpToolList(&count);
std::vector<Ref<Tool>> result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
result.push_back(new Tool(BNNewMcpToolReference(tools[i])));
BNFreeMcpToolList(tools, count);
return result;
}
Ref<Tool> Tool::GetByName(const std::string& name)
{
BNMcpTool* tool = BNGetMcpToolByName(name.c_str());
return tool ? new Tool(tool) : nullptr;
}
std::string Tool::GetName() const
{
return TakeString(BNGetMcpToolName(m_object));
}
std::string Tool::GetTitle() const
{
return TakeString(BNGetMcpToolTitle(m_object));
}
std::string Tool::GetDescription() const
{
return TakeString(BNGetMcpToolDescription(m_object));
}
std::string Tool::GetInputSchema() const
{
return TakeString(BNGetMcpToolInputSchema(m_object));
}
std::string Tool::GetOutputSchema() const
{
return TakeString(BNGetMcpToolOutputSchema(m_object));
}
Scope Tool::GetScope() const
{
return static_cast<Scope>(BNGetMcpToolScope(m_object));
}
uint32_t Tool::GetAnnotations() const
{
return BNGetMcpToolAnnotations(m_object);
}
std::string BinaryNinja::MCP::InvokeTool(
Tool& tool, ToolCallHost& host, const std::string& arguments, const ToolResult* error)
{
BNMcpToolCallCallbacks callbacks = {};
callbacks.context = &host;
callbacks.getBinaryView = [](void* ctxt) -> BNBinaryView* {
try
{
Ref<BinaryView> view = static_cast<ToolCallHost*>(ctxt)->GetBinaryView();
return view ? BNNewViewReference(view->GetObject()) : nullptr;
}
catch (const std::exception& e)
{
GetMcpLogger()->LogErrorForExceptionF(e, "MCP host failed to supply a binary view: {}", e.what());
return nullptr;
}
};
callbacks.isCancelled = [](void* ctxt) {
try
{
return static_cast<ToolCallHost*>(ctxt)->IsCancelled();
}
catch (const std::exception& e)
{
GetMcpLogger()->LogErrorForExceptionF(e, "MCP host failed to report cancellation: {}", e.what());
return false;
}
};
callbacks.reportProgress = [](void* ctxt, double progress, double total, const char* message) {
try
{
static_cast<ToolCallHost*>(ctxt)->ReportProgress(progress, total, message);
}
catch (const std::exception& e)
{
GetMcpLogger()->LogErrorForExceptionF(e, "MCP host failed to receive progress: {}", e.what());
}
};
BNMcpToolCall* call = BNCreateMcpToolCall(&callbacks);
BNMcpToolResult* result = BNCreateMcpToolResult();
if (error)
error->ApplyTo(result);
BNInvokeMcpTool(tool.GetObject(), call, arguments.c_str(), result);
std::string json = TakeString(BNGetMcpToolResultJson(result));
BNFreeMcpToolResult(result);
BNFreeMcpToolCall(call);
return json;
}
Ref<Tool> BinaryNinja::MCP::RegisterTool(
const ToolDefinition& definition, const std::string& inputSchema, ToolHandler handler)
{
return CreateToolUsing(definition, inputSchema, std::move(handler), BNRegisterMcpTool);
}
Ref<Tool> BinaryNinja::MCP::CreateTool(ToolSpec spec)
{
return CreateToolUsing(spec.definition, spec.inputSchema, std::move(spec.handler), BNCreateMcpTool);
}
void detail::AddProperty(
rapidjson::Value& properties, std::string_view name, rapidjson::Value& schema, Allocator& allocator)
{
properties.AddMember(rapidjson::Value(name.data(), name.size(), allocator), schema, allocator);
}
rapidjson::Value detail::TypeSchema(const char* type, Allocator& allocator)
{
rapidjson::Value schema(rapidjson::kObjectType);
schema.AddMember("type", rapidjson::StringRef(type), allocator);
return schema;
}
void detail::AddDescription(rapidjson::Value& schema, std::string_view description, Allocator& allocator)
{
if (description.empty())
return;
schema.AddMember("description", rapidjson::Value(description.data(), description.size(), allocator), allocator);
}
std::string detail::MissingMessage(std::string_view kind, std::string_view name)
{
return fmt::format("Expected {} parameter '{}'", kind, name);
}
std::string detail::SerializeJson(const rapidjson::Value& value)
{
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
value.Accept(writer);
return std::string(buffer.GetString(), buffer.GetSize());
}
std::optional<std::string> detail::FindUnexpectedArgument(
const rapidjson::Value& arguments, const std::vector<std::string>& names)
{
for (const auto& member : arguments.GetObj())
{
std::string_view name(member.name.GetString(), member.name.GetStringLength());
if (std::ranges::find(names, name) == names.end())
return fmt::format("Unexpected parameter '{}'", name);
}
return std::nullopt;
}
void detail::LogRejectedTool(std::string_view name, std::string_view reason)
{
GetMcpLogger()->LogErrorF("Rejected MCP tool '{}': {}", name, reason);
}
detail::InputSchemaBuilder::InputSchemaBuilder(std::string tool):
m_tool(std::move(tool)), m_schema(rapidjson::kObjectType), m_properties(rapidjson::kObjectType),
m_required(rapidjson::kArrayType)
{
}
void detail::InputSchemaBuilder::Add(
const std::string& name, bool required, const std::string* relativeTo, bool integer)
{
if (std::ranges::find(m_names, name) != m_names.end())
throw std::invalid_argument(fmt::format("MCP tool '{}' declares parameter '{}' more than once", m_tool, name));
if (relativeTo && std::ranges::find(m_integerNames, *relativeTo) == m_integerNames.end())
{
throw std::invalid_argument(fmt::format(
"MCP tool '{}' parameter '{}' is relative to '{}', which must be an integer parameter declared before it",
m_tool, name, *relativeTo));
}
m_names.push_back(name);
if (integer)
m_integerNames.push_back(name);
if (required)
m_required.PushBack(rapidjson::Value(name.c_str(), name.size(), GetAllocator()), GetAllocator());
}
std::string detail::InputSchemaBuilder::Build()
{
auto& allocator = GetAllocator();
m_schema.AddMember("type", "object", allocator);
m_schema.AddMember("properties", m_properties, allocator);
if (!m_required.Empty())
m_schema.AddMember("required", m_required, allocator);
m_schema.AddMember("additionalProperties", false, allocator);
return SerializeJson(m_schema);
}
String& String::NonEmpty()
{
m_nonEmpty = true;
return *this;
}
rapidjson::Value String::Schema(detail::Allocator& allocator) const
{
rapidjson::Value schema = detail::TypeSchema("string", allocator);
if (m_nonEmpty)
schema.AddMember("minLength", 1, allocator);
detail::AddDescription(schema, m_description, allocator);
return schema;
}
rapidjson::Value String::DefaultJson(std::string_view value, detail::Allocator& allocator) const
{
return rapidjson::Value(value.data(), value.size(), allocator);
}
std::string String::MissingMessage() const
{
return detail::MissingMessage(m_nonEmpty ? "non-empty string" : "string", m_name);
}
ArgumentResult<std::string> String::Convert(ToolCall&, const rapidjson::Value& value, const ConvertedArguments&) const
{
if (!value.IsString() || (m_nonEmpty && value.GetStringLength() == 0))
return bn::base::unexpected(MissingMessage());
return std::string(value.GetString(), value.GetStringLength());
}
Choice::Choice(std::string name, std::string description, std::vector<std::string> choices):
ValueParam(std::move(name), std::move(description)), m_choices(std::move(choices))
{
}
rapidjson::Value Choice::Schema(detail::Allocator& allocator) const
{
rapidjson::Value schema = detail::TypeSchema("string", allocator);
rapidjson::Value choices(rapidjson::kArrayType);
for (const auto& choice : m_choices)
choices.PushBack(rapidjson::Value(choice.c_str(), choice.size(), allocator), allocator);
schema.AddMember("enum", choices, allocator);
detail::AddDescription(schema, m_description, allocator);
return schema;
}
rapidjson::Value Choice::DefaultJson(std::string_view value, detail::Allocator& allocator) const
{
return rapidjson::Value(value.data(), value.size(), allocator);
}
std::string Choice::MissingMessage() const
{
return detail::MissingMessage("string", m_name);
}
ArgumentResult<std::string> Choice::Convert(ToolCall&, const rapidjson::Value& value, const ConvertedArguments&) const
{
if (!value.IsString())
return bn::base::unexpected(MissingMessage());
std::string text(value.GetString(), value.GetStringLength());
if (std::find(m_choices.begin(), m_choices.end(), text) == m_choices.end())
return bn::base::unexpected(fmt::format("Invalid enum value for parameter '{}'", m_name));
return text;
}
rapidjson::Value Bool::Schema(detail::Allocator& allocator) const
{
rapidjson::Value schema = detail::TypeSchema("boolean", allocator);
detail::AddDescription(schema, m_description, allocator);
return schema;
}
rapidjson::Value Bool::DefaultJson(bool value, detail::Allocator&) const
{
return rapidjson::Value(value);
}
std::string Bool::MissingMessage() const
{
return detail::MissingMessage("boolean", m_name);
}
ArgumentResult<bool> Bool::Convert(ToolCall&, const rapidjson::Value& value, const ConvertedArguments&) const
{
if (!value.IsBool())
return bn::base::unexpected(MissingMessage());
return value.GetBool();
}
UInt& UInt::Maximum(uint64_t maximum)
{
m_maximum = maximum;
m_clamp = false;
return *this;
}
UInt& UInt::ClampTo(uint64_t maximum)
{
m_maximum = maximum;
m_clamp = true;
return *this;
}
rapidjson::Value UInt::Schema(detail::Allocator& allocator) const
{
rapidjson::Value schema = detail::TypeSchema("integer", allocator);
schema.AddMember("minimum", 0, allocator);
if (m_maximum)
schema.AddMember("maximum", rapidjson::Value().SetUint64(*m_maximum), allocator);
detail::AddDescription(schema, m_description, allocator);
return schema;
}
rapidjson::Value UInt::DefaultJson(uint64_t value, detail::Allocator&) const
{
return rapidjson::Value(value);
}
std::string UInt::MissingMessage() const
{
return detail::MissingMessage("unsigned integer", m_name);
}
ArgumentResult<uint64_t> UInt::Convert(ToolCall&, const rapidjson::Value& value, const ConvertedArguments&) const
{
if (!value.IsUint64())
return bn::base::unexpected(MissingMessage());
uint64_t result = value.GetUint64();
if (m_maximum && result > *m_maximum)
{
if (m_clamp)
return *m_maximum;
return bn::base::unexpected(
fmt::format("Invalid unsigned integer parameter '{}': Must be at most {}", m_name, *m_maximum));
}
return result;
}
Int& Int::Minimum(int64_t minimum)
{
m_minimum = minimum;
m_clamp = false;
return *this;
}
Int& Int::Maximum(int64_t maximum)
{
m_maximum = maximum;
m_clamp = false;
return *this;
}
Int& Int::ClampTo(int64_t minimum, int64_t maximum)
{
m_minimum = minimum;
m_maximum = maximum;
m_clamp = true;
return *this;
}
rapidjson::Value Int::Schema(detail::Allocator& allocator) const
{
rapidjson::Value schema = detail::TypeSchema("integer", allocator);
if (m_minimum)
schema.AddMember("minimum", rapidjson::Value().SetInt64(*m_minimum), allocator);
if (m_maximum)
schema.AddMember("maximum", rapidjson::Value().SetInt64(*m_maximum), allocator);
detail::AddDescription(schema, m_description, allocator);
return schema;
}
rapidjson::Value Int::DefaultJson(int64_t value, detail::Allocator&) const
{
return rapidjson::Value(value);
}
std::string Int::MissingMessage() const
{
return detail::MissingMessage("integer", m_name);
}
ArgumentResult<int64_t> Int::Convert(ToolCall&, const rapidjson::Value& value, const ConvertedArguments&) const
{
if (!value.IsInt64())
return bn::base::unexpected(MissingMessage());
int64_t result = value.GetInt64();
if (m_minimum && result < *m_minimum)
{
if (m_clamp)
return *m_minimum;
return bn::base::unexpected(
fmt::format("Invalid integer parameter '{}': Must be at least {}", m_name, *m_minimum));
}
if (m_maximum && result > *m_maximum)
{
if (m_clamp)
return *m_maximum;
return bn::base::unexpected(
fmt::format("Invalid integer parameter '{}': Must be at most {}", m_name, *m_maximum));
}
return result;
}
rapidjson::Value Number::Schema(detail::Allocator& allocator) const
{
rapidjson::Value schema = detail::TypeSchema("number", allocator);
detail::AddDescription(schema, m_description, allocator);
return schema;
}
rapidjson::Value Number::DefaultJson(double value, detail::Allocator&) const
{
return rapidjson::Value(value);
}
std::string Number::MissingMessage() const
{
return detail::MissingMessage("number", m_name);
}
ArgumentResult<double> Number::Convert(ToolCall&, const rapidjson::Value& value, const ConvertedArguments&) const
{
if (!value.IsNumber())
return bn::base::unexpected(MissingMessage());
return value.GetDouble();
}
rapidjson::Value Address::Schema(detail::Allocator& allocator) const
{
rapidjson::Value schema = detail::TypeSchema("string", allocator);
detail::AddDescription(schema, m_description, allocator);
return schema;
}
rapidjson::Value Address::DefaultJson(uint64_t value, detail::Allocator& allocator) const
{
std::string text = fmt::format("{:#x}", value);
return rapidjson::Value(text.c_str(), text.size(), allocator);
}
std::string Address::MissingMessage() const
{
return detail::MissingMessage("address expression", m_name);
}
ArgumentResult<uint64_t> Address::Convert(
ToolCall& call, const rapidjson::Value& value, const ConvertedArguments&) const
{
auto address = call.ParseAddress(value);
if (!address)
return bn::base::unexpected(
fmt::format("Invalid address expression parameter '{}': {}", m_name, address.error()));
return address;
}
rapidjson::Value IntegerExpression::Schema(detail::Allocator& allocator) const
{
rapidjson::Value schema(rapidjson::kObjectType);
rapidjson::Value types(rapidjson::kArrayType);
types.PushBack(rapidjson::StringRef("integer"), allocator);
types.PushBack(rapidjson::StringRef("string"), allocator);
schema.AddMember("type", types, allocator);
schema.AddMember("minimum", 0, allocator);
detail::AddDescription(schema, m_description, allocator);
return schema;
}
rapidjson::Value IntegerExpression::DefaultJson(uint64_t value, detail::Allocator&) const
{
return rapidjson::Value(value);
}
std::string IntegerExpression::MissingMessage() const
{
return detail::MissingMessage("unsigned integer", m_name);
}
IntegerExpression& IntegerExpression::RelativeTo(std::string name)
{
m_relativeTo = std::move(name);
return *this;
}
ArgumentResult<uint64_t> IntegerExpression::Convert(
ToolCall& call, const rapidjson::Value& value, const ConvertedArguments& converted) const
{
uint64_t here = m_relativeTo ? converted.Get(*m_relativeTo).value_or(0) : 0;
auto integer = call.ParseInteger(value, here);
if (!integer)
return bn::base::unexpected(
fmt::format("Invalid unsigned integer parameter '{}': {}", m_name, integer.error()));
return integer;
}
JsonValue::JsonValue(std::string name, std::string schema): Param(std::move(name), ""), m_schema(std::move(schema)) {}
JsonValue JsonValue::Optional() const
{
JsonValue result = *this;
result.m_required = false;
return result;
}
void JsonValue::AddSchema(rapidjson::Value& properties, detail::Allocator& allocator) const
{
rapidjson::Document fragment;
try
{
fragment.Parse(m_schema.c_str());
}
catch (const ParseException&)
{
throw std::invalid_argument(fmt::format("The schema for MCP tool parameter '{}' is not valid JSON", m_name));
}
rapidjson::Value schema(fragment, allocator);
detail::AddProperty(properties, m_name, schema, allocator);
}
ArgumentResult<const rapidjson::Value*> JsonValue::Absent() const
{
if (m_required)
return bn::base::unexpected(MissingMessage());
return nullptr;
}
std::string JsonValue::MissingMessage() const
{
return fmt::format("Expected parameter '{}'", m_name);
}
ArgumentResult<const rapidjson::Value*> JsonValue::Convert(
ToolCall&, const rapidjson::Value& value, const ConvertedArguments&) const
{
return &value;
}