types.vala 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * Copyright (C) 2025 Mcp-Vala Project
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. *
  18. * Author: Mcp-Vala Project
  19. */
  20. /**
  21. * Tool-related data types for MCP protocol.
  22. *
  23. * This namespace contains all tool-related types defined by the MCP specification,
  24. * including tool definitions, execution results, and related structures.
  25. */
  26. namespace Mcp.Tools.Types {
  27. /**
  28. * Tool definition as defined by MCP protocol.
  29. */
  30. public class ToolDefinition : GLib.Object {
  31. /**
  32. * Name of the tool.
  33. */
  34. public string name { get; set; }
  35. /**
  36. * Optional title of the tool.
  37. */
  38. public string? title { get; set; }
  39. /**
  40. * Optional description of the tool.
  41. */
  42. public string? description { get; set; }
  43. /**
  44. * JSON schema for tool input.
  45. */
  46. public Variant input_schema { get; set; }
  47. /**
  48. * Optional JSON schema for tool output.
  49. */
  50. public Variant? output_schema { get; set; }
  51. /**
  52. * Optional execution settings.
  53. */
  54. public ToolExecution? execution { get; set; }
  55. /**
  56. * Optional annotations for the tool.
  57. */
  58. public ToolAnnotations? annotations { get; set; }
  59. /**
  60. * Creates a new ToolDefinition.
  61. *
  62. * @param name Name of the tool
  63. * @param input_schema JSON schema for input
  64. */
  65. public ToolDefinition (string name, Variant input_schema) {
  66. this.name = name;
  67. this.input_schema = input_schema;
  68. }
  69. /**
  70. * Serializes ToolDefinition to GLib.Variant.
  71. *
  72. * @return A GLib.Variant representing this ToolDefinition
  73. */
  74. public Variant to_variant () {
  75. var builder = Mcp.Types.VariantUtils.new_dict_builder ();
  76. builder.add ("{sv}", "name", new Variant.string (name));
  77. Mcp.Types.VariantUtils.add_string_if_not_null (builder, "title", title);
  78. Mcp.Types.VariantUtils.add_string_if_not_null (builder, "description", description);
  79. builder.add ("{sv}", "inputSchema", input_schema);
  80. Mcp.Types.VariantUtils.add_variant_if_not_null (builder, "outputSchema", output_schema);
  81. Mcp.Types.VariantUtils.add_variant_if_not_null (builder, "execution",
  82. execution != null ? execution.to_variant () : null);
  83. Mcp.Types.VariantUtils.add_variant_if_not_null (builder, "annotations",
  84. annotations != null ? annotations.to_variant () : null);
  85. return builder.end ();
  86. }
  87. /**
  88. * Creates a ToolDefinition from GLib.Variant.
  89. *
  90. * @param variant The GLib.Variant to deserialize
  91. * @return The deserialized ToolDefinition
  92. * @throws Error If deserialization fails
  93. */
  94. public ToolDefinition.from_variant (Variant variant) throws Error {
  95. if (!variant.is_of_type (VariantType.VARDICT)) {
  96. throw new Mcp.Core.McpError.PARSE_ERROR ("ToolDefinition must be a dictionary");
  97. }
  98. if (variant.lookup_value ("name", null) == null) {
  99. throw new Mcp.Core.McpError.INVALID_REQUEST ("Missing name in ToolDefinition");
  100. }
  101. if (variant.lookup_value ("inputSchema", null) == null) {
  102. throw new Mcp.Core.McpError.INVALID_REQUEST ("Missing inputSchema in ToolDefinition");
  103. }
  104. this.name = variant.lookup_value ("name", VariantType.STRING).get_string ();
  105. if (variant.lookup_value ("title", null) != null) {
  106. this.title = variant.lookup_value ("title", VariantType.STRING).get_string ();
  107. }
  108. if (variant.lookup_value ("description", null) != null) {
  109. this.description = variant.lookup_value ("description", VariantType.STRING).get_string ();
  110. }
  111. this.input_schema = variant.lookup_value ("inputSchema", VariantType.VARDICT);
  112. if (variant.lookup_value ("outputSchema", null) != null) {
  113. this.output_schema = variant.lookup_value ("outputSchema", VariantType.VARDICT);
  114. }
  115. if (variant.lookup_value ("execution", null) != null) {
  116. this.execution = new ToolExecution.from_variant (variant.lookup_value ("execution", VariantType.VARDICT));
  117. }
  118. if (variant.lookup_value ("annotations", null) != null) {
  119. this.annotations = new ToolAnnotations.from_variant (variant.lookup_value ("annotations", VariantType.VARDICT));
  120. }
  121. }
  122. }
  123. /**
  124. * Tool execution settings with enhanced context and state management.
  125. */
  126. public class ToolExecution : GLib.Object {
  127. /**
  128. * Task support level.
  129. */
  130. public string task_support { get; set; default = "forbidden"; }
  131. /**
  132. * Whether the tool supports progress reporting.
  133. */
  134. public bool supports_progress { get; set; default = false; }
  135. /**
  136. * Whether the tool supports cancellation.
  137. */
  138. public bool supports_cancellation { get; set; default = false; }
  139. /**
  140. * Maximum execution time in seconds (0 for no limit).
  141. */
  142. public int timeout_seconds { get; set; default = 30; }
  143. /**
  144. * Creates a new ToolExecution.
  145. *
  146. * @param task_support Task support level
  147. * @param supports_progress Whether progress reporting is supported
  148. * @param supports_cancellation Whether cancellation is supported
  149. * @param timeout_seconds Maximum execution time in seconds
  150. */
  151. public ToolExecution (string task_support = "forbidden",
  152. bool supports_progress = false,
  153. bool supports_cancellation = false,
  154. int timeout_seconds = 30) {
  155. this.task_support = task_support;
  156. this.supports_progress = supports_progress;
  157. this.supports_cancellation = supports_cancellation;
  158. this.timeout_seconds = timeout_seconds;
  159. }
  160. /**
  161. * Serializes ToolExecution to GLib.Variant.
  162. *
  163. * @return A GLib.Variant representing this ToolExecution
  164. */
  165. public Variant to_variant () {
  166. var builder = Mcp.Types.VariantUtils.new_dict_builder ();
  167. builder.add ("{sv}", "taskSupport", new Variant.string (task_support));
  168. if (supports_progress) {
  169. builder.add ("{sv}", "supportsProgress", new Variant.boolean (supports_progress));
  170. }
  171. if (supports_cancellation) {
  172. builder.add ("{sv}", "supportsCancellation", new Variant.boolean (supports_cancellation));
  173. }
  174. if (timeout_seconds > 0) {
  175. builder.add ("{sv}", "timeoutSeconds", new Variant.int32 (timeout_seconds));
  176. }
  177. return builder.end ();
  178. }
  179. /**
  180. * Creates a ToolExecution from GLib.Variant.
  181. *
  182. * @param variant The GLib.Variant to deserialize
  183. * @return The deserialized ToolExecution
  184. * @throws Error If deserialization fails
  185. */
  186. public ToolExecution.from_variant (Variant variant) throws Error {
  187. if (!variant.is_of_type (VariantType.VARDICT)) {
  188. throw new Mcp.Core.McpError.PARSE_ERROR ("ToolExecution must be a dictionary");
  189. }
  190. if (variant.lookup_value ("taskSupport", null) != null) {
  191. this.task_support = variant.lookup_value ("taskSupport", VariantType.STRING).get_string ();
  192. }
  193. if (variant.lookup_value ("supportsProgress", null) != null) {
  194. this.supports_progress = variant.lookup_value ("supportsProgress", VariantType.BOOLEAN).get_boolean ();
  195. }
  196. if (variant.lookup_value ("supportsCancellation", null) != null) {
  197. this.supports_cancellation = variant.lookup_value ("supportsCancellation", VariantType.BOOLEAN).get_boolean ();
  198. }
  199. if (variant.lookup_value ("timeoutSeconds", null) != null) {
  200. this.timeout_seconds = variant.lookup_value ("timeoutSeconds", VariantType.INT32).get_int32 ();
  201. }
  202. }
  203. }
  204. /**
  205. * Tool execution context for preserving state during execution.
  206. */
  207. public class ToolExecutionContext : GLib.Object {
  208. /**
  209. * Unique identifier for this execution context.
  210. */
  211. public string execution_id { get; set; }
  212. /**
  213. * The tool name being executed.
  214. */
  215. public string tool_name { get; set; }
  216. /**
  217. * The arguments passed to the tool.
  218. */
  219. public Variant arguments { get; set; }
  220. /**
  221. * Progress token for reporting progress.
  222. */
  223. public string? progress_token { get; set; }
  224. /**
  225. * Current execution state.
  226. */
  227. public ExecutionState state { get; set; default = ExecutionState.INITIALIZED; }
  228. /**
  229. * Timestamp when execution started.
  230. */
  231. public DateTime? start_time { get; set; }
  232. /**
  233. * Timestamp when execution ended.
  234. */
  235. public DateTime? end_time { get; set; }
  236. /**
  237. * Whether execution has been cancelled.
  238. */
  239. public bool is_cancelled { get; set; default = false; }
  240. /**
  241. * Cancellation source for async operations.
  242. */
  243. private GLib.Cancellable cancellable;
  244. /**
  245. * Creates a new ToolExecutionContext.
  246. *
  247. * @param execution_id Unique identifier for the execution
  248. * @param tool_name The tool name being executed
  249. * @param arguments The arguments passed to the tool
  250. * @param progress_token Optional progress token
  251. */
  252. public ToolExecutionContext (string execution_id, string tool_name,
  253. Variant arguments, string? progress_token = null) {
  254. this.execution_id = execution_id;
  255. this.tool_name = tool_name;
  256. this.arguments = arguments;
  257. this.progress_token = progress_token;
  258. this.cancellable = new GLib.Cancellable ();
  259. }
  260. /**
  261. * Gets the cancellable for this execution context.
  262. *
  263. * @return The cancellable object
  264. */
  265. public GLib.Cancellable get_cancellable () {
  266. return cancellable;
  267. }
  268. /**
  269. * Cancels the execution.
  270. */
  271. public void cancel () {
  272. is_cancelled = true;
  273. state = ExecutionState.CANCELLED;
  274. cancellable.cancel ();
  275. }
  276. /**
  277. * Marks the execution as started.
  278. */
  279. public void mark_started () {
  280. start_time = new DateTime.now_utc ();
  281. state = ExecutionState.RUNNING;
  282. }
  283. /**
  284. * Marks the execution as completed.
  285. */
  286. public void mark_completed () {
  287. end_time = new DateTime.now_utc ();
  288. if (state != ExecutionState.CANCELLED) {
  289. state = ExecutionState.COMPLETED;
  290. }
  291. }
  292. /**
  293. * Marks the execution as failed.
  294. */
  295. public void mark_failed () {
  296. end_time = new DateTime.now_utc ();
  297. state = ExecutionState.FAILED;
  298. }
  299. /**
  300. * Gets the execution duration in milliseconds.
  301. *
  302. * @return The duration in milliseconds, or 0 if not available
  303. */
  304. public int64 get_duration_ms () {
  305. if (start_time == null || end_time == null) {
  306. return 0;
  307. }
  308. return (int64) (end_time.difference (start_time) / 1000);
  309. }
  310. }
  311. /**
  312. * Execution states for tools.
  313. */
  314. public enum ExecutionState {
  315. /**
  316. * Execution has been initialized but not started.
  317. */
  318. INITIALIZED,
  319. /**
  320. * Execution is currently running.
  321. */
  322. RUNNING,
  323. /**
  324. * Execution completed successfully.
  325. */
  326. COMPLETED,
  327. /**
  328. * Execution failed.
  329. */
  330. FAILED,
  331. /**
  332. * Execution was cancelled.
  333. */
  334. CANCELLED
  335. }
  336. /**
  337. * Progress information for tool execution.
  338. */
  339. public class ToolProgress : GLib.Object {
  340. /**
  341. * Progress token identifying this progress update.
  342. */
  343. public string progress_token { get; set; }
  344. /**
  345. * Current progress value (0-100).
  346. */
  347. public double progress { get; set; }
  348. /**
  349. * Optional progress message.
  350. */
  351. public string? message { get; set; }
  352. /**
  353. * Creates a new ToolProgress.
  354. *
  355. * @param progress_token Progress token
  356. * @param progress Current progress value (0-100)
  357. * @param message Optional progress message
  358. */
  359. public ToolProgress (string progress_token, double progress, string? message = null) {
  360. this.progress_token = progress_token;
  361. this.progress = progress;
  362. this.message = message;
  363. }
  364. /**
  365. * Serializes ToolProgress to GLib.Variant.
  366. *
  367. * @return A GLib.Variant representing this ToolProgress
  368. */
  369. public Variant to_variant () {
  370. var builder = Mcp.Types.VariantUtils.new_dict_builder ();
  371. builder.add ("{sv}", "progressToken", new Variant.string (progress_token));
  372. builder.add ("{sv}", "progress", new Variant.double (progress));
  373. Mcp.Types.VariantUtils.add_string_if_not_null (builder, "message", message);
  374. return builder.end ();
  375. }
  376. }
  377. /**
  378. * Tool annotations.
  379. */
  380. public class ToolAnnotations : GLib.Object {
  381. /**
  382. * Optional audience for this tool.
  383. */
  384. public string? audience { get; set; }
  385. /**
  386. * Optional priority of this tool.
  387. */
  388. public double? priority { get; set; }
  389. /**
  390. * Creates a new ToolAnnotations.
  391. */
  392. public ToolAnnotations () {
  393. }
  394. /**
  395. * Serializes ToolAnnotations to GLib.Variant.
  396. *
  397. * @return A GLib.Variant representing this ToolAnnotations
  398. */
  399. public Variant to_variant () {
  400. var builder = Mcp.Types.VariantUtils.new_dict_builder ();
  401. Mcp.Types.VariantUtils.add_string_if_not_null (builder, "audience", audience);
  402. if (priority != null) {
  403. builder.add ("{sv}", "priority", new Variant.double (priority));
  404. }
  405. return builder.end ();
  406. }
  407. /**
  408. * Creates ToolAnnotations from GLib.Variant.
  409. *
  410. * @param variant The GLib.Variant to deserialize
  411. * @return The deserialized ToolAnnotations
  412. * @throws Error If deserialization fails
  413. */
  414. public ToolAnnotations.from_variant (Variant variant) throws Error {
  415. if (!variant.is_of_type (VariantType.VARDICT)) {
  416. throw new Mcp.Core.McpError.PARSE_ERROR ("ToolAnnotations must be a dictionary");
  417. }
  418. if (variant.lookup_value ("audience", null) != null) {
  419. this.audience = variant.lookup_value ("audience", VariantType.STRING).get_string ();
  420. }
  421. if (variant.lookup_value ("priority", null) != null) {
  422. this.priority = variant.lookup_value ("priority", VariantType.DOUBLE).get_double ();
  423. }
  424. }
  425. }
  426. /**
  427. * Result of calling a tool.
  428. */
  429. public class CallToolResult : GLib.Object {
  430. /**
  431. * List of content blocks in the result.
  432. */
  433. public Gee.ArrayList<Mcp.Types.Common.ContentBlock> content { get; set; }
  434. /**
  435. * Optional structured content data.
  436. * This field is used for structured data that doesn't fit into content blocks,
  437. * such as JSON objects, arrays, or other structured data formats.
  438. */
  439. public Variant? structured_content { get; set; }
  440. /**
  441. * Whether this result represents an error.
  442. */
  443. public bool is_error { get; set; default = false; }
  444. /**
  445. * Optional metadata about the result.
  446. */
  447. public Variant? metadata { get; set; }
  448. /**
  449. * Optional pagination cursor for results that support pagination.
  450. */
  451. public string? next_cursor { get; set; }
  452. /**
  453. * Creates a new CallToolResult.
  454. */
  455. public CallToolResult () {
  456. content = new Gee.ArrayList<Mcp.Types.Common.ContentBlock> ();
  457. }
  458. /**
  459. * Creates a new CallToolResult with content.
  460. *
  461. * @param content_blocks Initial content blocks
  462. * @param is_error Whether this result represents an error
  463. */
  464. public CallToolResult.with_content (Gee.ArrayList<Mcp.Types.Common.ContentBlock> content_blocks, bool is_error = false) {
  465. this.content = content_blocks;
  466. this.is_error = is_error;
  467. }
  468. /**
  469. * Creates a new CallToolResult with structured content.
  470. *
  471. * @param structured_data The structured content data
  472. * @param text_description Optional text description
  473. * @param is_error Whether this result represents an error
  474. */
  475. public CallToolResult.with_structured (Variant structured_data, string? text_description = null, bool is_error = false) {
  476. this.content = new Gee.ArrayList<Mcp.Types.Common.ContentBlock> ();
  477. this.structured_content = structured_data;
  478. this.is_error = is_error;
  479. if (text_description != null) {
  480. var text_content = new Mcp.Types.Common.TextContent (text_description);
  481. this.content.add (text_content);
  482. }
  483. }
  484. /**
  485. * Serializes CallToolResult to GLib.Variant.
  486. *
  487. * @return A GLib.Variant representing this CallToolResult
  488. */
  489. public Variant to_variant () {
  490. var builder = Mcp.Types.VariantUtils.new_dict_builder ();
  491. // Serialize content array
  492. var content_builder = Mcp.Types.VariantUtils.new_dict_array_builder ();
  493. foreach (var content_block in content) {
  494. content_builder.add_value (content_block.to_variant ());
  495. }
  496. builder.add ("{sv}", "content", content_builder.end ());
  497. // Add structured content if present
  498. Mcp.Types.VariantUtils.add_variant_if_not_null (builder, "structuredContent", structured_content);
  499. // Add error flag
  500. builder.add ("{sv}", "isError", new Variant.boolean (is_error));
  501. // Add metadata if present
  502. Mcp.Types.VariantUtils.add_variant_if_not_null (builder, "metadata", metadata);
  503. // Add pagination cursor if present
  504. Mcp.Types.VariantUtils.add_string_if_not_null (builder, "nextCursor", next_cursor);
  505. return builder.end ();
  506. }
  507. /**
  508. * Creates a CallToolResult from GLib.Variant.
  509. *
  510. * @param variant The GLib.Variant to deserialize
  511. * @return The deserialized CallToolResult
  512. * @throws Error If deserialization fails
  513. */
  514. public CallToolResult.from_variant (Variant variant) throws Error {
  515. if (!variant.is_of_type (VariantType.VARDICT)) {
  516. throw new Mcp.Core.McpError.PARSE_ERROR ("CallToolResult must be a dictionary");
  517. }
  518. if (variant.lookup_value ("content", null) == null) {
  519. throw new Mcp.Core.McpError.INVALID_REQUEST ("Missing content in CallToolResult");
  520. }
  521. this.content = new Gee.ArrayList<Mcp.Types.Common.ContentBlock> ();
  522. var content = variant.lookup_value ("content", new VariantType ("aa{sv}"));
  523. for (size_t i = 0; i < content.n_children (); i++) {
  524. var element = content.get_child_value (i);
  525. if (element.is_of_type (VariantType.VARDICT)) {
  526. var content_block = deserialize_content_block (element);
  527. if (content_block != null) {
  528. this.content.add (content_block);
  529. }
  530. }
  531. }
  532. // Parse structured content if present
  533. if (variant.lookup_value ("structuredContent", null) != null) {
  534. this.structured_content = variant.lookup_value ("structuredContent", null);
  535. }
  536. // Parse error flag if present
  537. if (variant.lookup_value ("isError", null) != null) {
  538. this.is_error = variant.lookup_value ("isError", VariantType.BOOLEAN).get_boolean ();
  539. }
  540. // Parse metadata if present
  541. if (variant.lookup_value ("metadata", null) != null) {
  542. this.metadata = variant.lookup_value ("metadata", null);
  543. }
  544. // Parse pagination cursor if present
  545. if (variant.lookup_value ("nextCursor", null) != null) {
  546. this.next_cursor = variant.lookup_value ("nextCursor", VariantType.STRING).get_string ();
  547. }
  548. }
  549. /**
  550. * Deserializes a content block from a Variant.
  551. *
  552. * @param variant The Variant to deserialize
  553. * @return The deserialized ContentBlock or null if type is unknown
  554. * @throws Error If deserialization fails
  555. */
  556. private Mcp.Types.Common.ContentBlock? deserialize_content_block (Variant variant) throws Error {
  557. if (!variant.is_of_type (VariantType.VARDICT)) {
  558. return null;
  559. }
  560. if (variant.lookup_value ("type", null) == null) {
  561. throw new Mcp.Core.McpError.INVALID_REQUEST ("Content block missing type field");
  562. }
  563. string type = variant.lookup_value ("type", VariantType.STRING).get_string ();
  564. switch (type) {
  565. case "text":
  566. return new Mcp.Types.Common.TextContent.from_variant (variant);
  567. case "image":
  568. return new Mcp.Types.Common.ImageContent.from_variant (variant);
  569. case "audio":
  570. return new Mcp.Types.Common.AudioContent.from_variant (variant);
  571. case "resource_link":
  572. return new Mcp.Types.Common.ResourceLink.from_variant (variant);
  573. case "resource":
  574. return new Mcp.Types.Common.EmbeddedResource.from_variant (variant);
  575. default:
  576. // Unknown content type, create a generic text content with warning
  577. return new Mcp.Types.Common.TextContent (
  578. "Warning: Unknown content type '%s'".printf (type)
  579. );
  580. }
  581. }
  582. /**
  583. * Adds a text content block to result.
  584. *
  585. * @param text The text content to add
  586. */
  587. public void add_text (string text) {
  588. var text_content = new Mcp.Types.Common.TextContent (text);
  589. content.add (text_content);
  590. }
  591. /**
  592. * Adds an image content block to result.
  593. *
  594. * @param data Base64 encoded image data
  595. * @param mime_type MIME type of image
  596. */
  597. public void add_image (string data, string mime_type) {
  598. var image_content = new Mcp.Types.Common.ImageContent (data, mime_type);
  599. content.add (image_content);
  600. }
  601. /**
  602. * Adds a resource link to result.
  603. *
  604. * @param uri URI of resource
  605. * @param name Name of resource
  606. */
  607. public void add_resource_link (string uri, string name) {
  608. var resource_link = new Mcp.Types.Common.ResourceLink (uri, name);
  609. content.add (resource_link);
  610. }
  611. /**
  612. * Sets structured content and optionally adds a text description.
  613. *
  614. * @param structured_data The structured content data
  615. * @param description Optional text description
  616. */
  617. public void update_structured_content (Variant structured_data, string? description = null) {
  618. this.structured_content = structured_data;
  619. if (description != null) {
  620. add_text (description);
  621. }
  622. }
  623. /**
  624. * Sets metadata for result.
  625. *
  626. * @param metadata The metadata to set
  627. */
  628. public void update_metadata (Variant metadata) {
  629. this.metadata = metadata;
  630. }
  631. /**
  632. * Sets pagination cursor for result.
  633. *
  634. * @param cursor The pagination cursor
  635. */
  636. public void update_next_cursor (string cursor) {
  637. this.next_cursor = cursor;
  638. }
  639. /**
  640. * Creates a successful result with text content.
  641. *
  642. * @param text The text content
  643. * @return A successful CallToolResult
  644. */
  645. public static CallToolResult success (string text) {
  646. var result = new CallToolResult ();
  647. result.add_text (text);
  648. return result;
  649. }
  650. /**
  651. * Creates an error result with text content.
  652. *
  653. * @param error_message The error message
  654. * @return An error CallToolResult
  655. */
  656. public static CallToolResult error (string error_message) {
  657. var result = new CallToolResult ();
  658. result.add_text (error_message);
  659. result.is_error = true;
  660. return result;
  661. }
  662. /**
  663. * Creates a successful result with structured content.
  664. *
  665. * @param structured_data The structured content data
  666. * @param description Optional text description
  667. * @return A successful CallToolResult with structured content
  668. */
  669. public static CallToolResult success_structured (Variant structured_data, string? description = null) {
  670. var result = new CallToolResult ();
  671. result.update_structured_content (structured_data, description);
  672. return result;
  673. }
  674. }
  675. }