11
2+ #include " ../json.hpp"
23#include " handler.hpp"
4+ #include " params.hpp"
35
46#ifdef LLAMA_USE_CURL
57# include " mcp_sse_transport.hpp"
911
1012using json = toolcall::json;
1113
12- toolcall::params::params (std::string tools, std::string choice) {
13- this ->tools (tools);
14- this ->choice (choice);
15- }
16-
17- static bool starts_with (const std::string & str, const std::string & prefix) {
18- return str.size () >= prefix.size ()
19- && str.compare (0 , prefix.size (), prefix) == 0 ;
20- }
21-
2214std::shared_ptr<toolcall::handler> toolcall::create_handler (const toolcall::params & params) {
2315 std::shared_ptr<toolcall::handler> result;
2416
@@ -42,62 +34,6 @@ std::shared_ptr<toolcall::handler> toolcall::create_handler(const toolcall::para
4234 return result;
4335}
4436
45- void toolcall::params::tools (std::string tools) {
46- try {
47-
48- if (tools.empty ()) {
49- tools_ = std::move (tools);
50-
51- } else if (starts_with (tools, " mcp+http" )) {
52- #ifdef LLAMA_USE_CURL
53- tools_ = std::move (tools);
54- #else
55- throw std::invalid_argument (
56- " Model Context Protocol (MCP) only works when llama.cpp is compiled with libcurl" );
57- #endif
58- } else {
59- tools_ = std::make_shared<json>(json::parse (tools));
60- auto tools_ptr = std::get<std::shared_ptr<json>>(tools_);
61- if (! tools_ptr->is_array ()) {
62- throw std::invalid_argument (
63- " tools must be a URL of the form \" mcp+http(s)://hostname[:port]/\" "
64- " , or a valid JSON array containing tool definitions" );
65- }
66- }
67-
68- } catch (const json::exception & err) {
69- throw std::invalid_argument (err.what ());
70- }
71- }
72-
73- void toolcall::params::choice (std::string choice) {
74- try {
75- if (choice == " auto" || choice == " required" || choice == " none" ) {
76- tool_choice_ = std::move (choice);
77-
78- } else {
79- auto choice_ptr = std::make_shared<json>(json::parse (choice));
80- tool_choice_ = choice_ptr;
81- if (! choice_ptr->is_object ()) {
82- throw std::invalid_argument (
83- " tool choice must be a valid JSON object, \" auto\" , \" required\" , or \" none\" " );
84- }
85- }
86-
87- } catch (const json::exception & err) {
88- throw std::invalid_argument (err.what ());
89- }
90- }
91-
92- toolcall::params::operator bool () const {
93- if (std::holds_alternative<std::string>(tools_)) {
94- return ! std::get<std::string>(tools_).empty ();
95-
96- } else {
97- return std::get<toolcall::json_ptr>(tools_) != nullptr ;
98- }
99- }
100-
10137json toolcall::handler::tool_list () {
10238 return impl_->tool_list ();
10339}
0 commit comments