protocol.vala 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. * MCP protocol types and data structures.
  22. *
  23. * This namespace contains all the protocol-specific types defined by the MCP specification,
  24. * including server information, capabilities, and other core protocol data structures.
  25. */
  26. namespace Mcp.Types.Protocol {
  27. /**
  28. * Server information as defined by the MCP protocol.
  29. */
  30. public class ServerInfo : GLib.Object {
  31. /**
  32. * The name of the server.
  33. */
  34. public string name { get; set; }
  35. /**
  36. * The version of the server.
  37. */
  38. public string version { get; set; }
  39. /**
  40. * Optional description of the server.
  41. */
  42. public string? description { get; set; }
  43. /**
  44. * Optional website URL for the server.
  45. */
  46. public string? website_url { get; set; }
  47. /**
  48. * Creates a new ServerInfo.
  49. *
  50. * @param name Server name
  51. * @param version Server version
  52. */
  53. public ServerInfo (string name, string version) {
  54. this.name = name;
  55. this.version = version;
  56. }
  57. /**
  58. * Serializes ServerInfo to GLib.Variant.
  59. *
  60. * @return A GLib.Variant representing this ServerInfo
  61. */
  62. public Variant to_variant () {
  63. var builder = Mcp.Types.VariantUtils.new_dict_builder ();
  64. builder.add ("{sv}", "name", new Variant.string (name));
  65. builder.add ("{sv}", "version", new Variant.string (version));
  66. Mcp.Types.VariantUtils.add_string_if_not_null (builder, "description", description);
  67. Mcp.Types.VariantUtils.add_string_if_not_null (builder, "website_url", website_url);
  68. return builder.end ();
  69. }
  70. /**
  71. * Creates a ServerInfo from GLib.Variant.
  72. *
  73. * @param variant The GLib.Variant to deserialize
  74. * @return The deserialized ServerInfo
  75. * @throws Error If deserialization fails
  76. */
  77. public ServerInfo.from_variant (Variant variant) throws Error {
  78. if (!variant.is_of_type (VariantType.VARDICT)) {
  79. throw new Mcp.Core.McpError.PARSE_ERROR ("ServerInfo must be a dictionary");
  80. }
  81. if (variant.lookup_value ("name", null) == null) {
  82. throw new Mcp.Core.McpError.INVALID_REQUEST ("Missing name in ServerInfo");
  83. }
  84. if (variant.lookup_value ("version", null) == null) {
  85. throw new Mcp.Core.McpError.INVALID_REQUEST ("Missing version in ServerInfo");
  86. }
  87. this.name = variant.lookup_value ("name", VariantType.STRING).get_string ();
  88. this.version = variant.lookup_value ("version", VariantType.STRING).get_string ();
  89. if (variant.lookup_value ("description", null) != null) {
  90. this.description = variant.lookup_value ("description", VariantType.STRING).get_string ();
  91. }
  92. if (variant.lookup_value ("website_url", null) != null) {
  93. this.website_url = variant.lookup_value ("website_url", VariantType.STRING).get_string ();
  94. }
  95. }
  96. }
  97. /**
  98. * Server capabilities as defined by the MCP protocol.
  99. */
  100. public class ServerCapabilities : GLib.Object {
  101. /**
  102. * Whether logging is supported.
  103. */
  104. public bool logging { get; set; default = false; }
  105. /**
  106. * Whether completions are supported.
  107. */
  108. public bool completions { get; set; default = false; }
  109. /**
  110. * Optional prompts capabilities.
  111. */
  112. public PromptsCapabilities? prompts { get; set; }
  113. /**
  114. * Optional resources capabilities.
  115. */
  116. public ResourcesCapabilities? resources { get; set; }
  117. /**
  118. * Optional tools capabilities.
  119. */
  120. public ToolsCapabilities? tools { get; set; }
  121. /**
  122. * Creates a new ServerCapabilities.
  123. */
  124. public ServerCapabilities () {
  125. }
  126. /**
  127. * Serializes ServerCapabilities to GLib.Variant.
  128. *
  129. * @return A GLib.Variant representing this ServerCapabilities
  130. */
  131. public Variant to_variant () {
  132. var builder = Mcp.Types.VariantUtils.new_dict_builder ();
  133. if (logging) {
  134. builder.add ("{sv}", "logging", new Variant.boolean (logging));
  135. }
  136. if (completions) {
  137. builder.add ("{sv}", "completions", new Variant.boolean (completions));
  138. }
  139. Mcp.Types.VariantUtils.add_variant_if_not_null (builder, "prompts",
  140. prompts != null ? prompts.to_variant () : null);
  141. Mcp.Types.VariantUtils.add_variant_if_not_null (builder, "resources",
  142. resources != null ? resources.to_variant () : null);
  143. Mcp.Types.VariantUtils.add_variant_if_not_null (builder, "tools",
  144. tools != null ? tools.to_variant () : null);
  145. return builder.end ();
  146. }
  147. /**
  148. * Creates a ServerCapabilities from GLib.Variant.
  149. *
  150. * @param variant The GLib.Variant to deserialize
  151. * @return The deserialized ServerCapabilities
  152. * @throws Error If deserialization fails
  153. */
  154. public ServerCapabilities.from_variant (Variant variant) throws Error {
  155. if (!variant.is_of_type (VariantType.VARDICT)) {
  156. throw new Mcp.Core.McpError.PARSE_ERROR ("ServerCapabilities must be a dictionary");
  157. }
  158. if (variant.lookup_value ("logging", null) != null) {
  159. this.logging = variant.lookup_value ("logging", VariantType.BOOLEAN).get_boolean ();
  160. }
  161. if (variant.lookup_value ("completions", null) != null) {
  162. this.completions = variant.lookup_value ("completions", VariantType.BOOLEAN).get_boolean ();
  163. }
  164. if (variant.lookup_value ("prompts", null) != null) {
  165. this.prompts = new PromptsCapabilities.from_variant (variant.lookup_value ("prompts", VariantType.VARDICT));
  166. }
  167. if (variant.lookup_value ("resources", null) != null) {
  168. this.resources = new ResourcesCapabilities.from_variant (variant.lookup_value ("resources", VariantType.VARDICT));
  169. }
  170. if (variant.lookup_value ("tools", null) != null) {
  171. this.tools = new ToolsCapabilities.from_variant (variant.lookup_value ("tools", VariantType.VARDICT));
  172. }
  173. }
  174. }
  175. /**
  176. * Prompts capabilities.
  177. */
  178. public class PromptsCapabilities : GLib.Object {
  179. /**
  180. * Whether the list of prompts can change.
  181. */
  182. public bool list_changed { get; set; default = false; }
  183. /**
  184. * Creates a new PromptsCapabilities.
  185. */
  186. public PromptsCapabilities () {
  187. }
  188. /**
  189. * Serializes PromptsCapabilities to GLib.Variant.
  190. *
  191. * @return A GLib.Variant representing this PromptsCapabilities
  192. */
  193. public Variant to_variant () {
  194. var builder = Mcp.Types.VariantUtils.new_dict_builder ();
  195. builder.add ("{sv}", "listChanged", new Variant.boolean (list_changed));
  196. return builder.end ();
  197. }
  198. /**
  199. * Creates a PromptsCapabilities from GLib.Variant.
  200. *
  201. * @param variant The GLib.Variant to deserialize
  202. * @return The deserialized PromptsCapabilities
  203. * @throws Error If deserialization fails
  204. */
  205. public PromptsCapabilities.from_variant (Variant variant) throws Error {
  206. if (!variant.is_of_type (VariantType.VARDICT)) {
  207. throw new Mcp.Core.McpError.PARSE_ERROR ("PromptsCapabilities must be a dictionary");
  208. }
  209. if (variant.lookup_value ("listChanged", null) != null) {
  210. this.list_changed = variant.lookup_value ("listChanged", VariantType.BOOLEAN).get_boolean ();
  211. }
  212. }
  213. }
  214. /**
  215. * Resources capabilities.
  216. */
  217. public class ResourcesCapabilities : GLib.Object {
  218. /**
  219. * Whether subscription is supported.
  220. */
  221. public bool subscribe { get; set; default = false; }
  222. /**
  223. * Whether the list of resources can change.
  224. */
  225. public bool list_changed { get; set; default = false; }
  226. /**
  227. * Creates a new ResourcesCapabilities.
  228. */
  229. public ResourcesCapabilities () {
  230. }
  231. /**
  232. * Serializes ResourcesCapabilities to GLib.Variant.
  233. *
  234. * @return A GLib.Variant representing this ResourcesCapabilities
  235. */
  236. public Variant to_variant () {
  237. var builder = Mcp.Types.VariantUtils.new_dict_builder ();
  238. builder.add ("{sv}", "subscribe", new Variant.boolean (subscribe));
  239. builder.add ("{sv}", "listChanged", new Variant.boolean (list_changed));
  240. return builder.end ();
  241. }
  242. /**
  243. * Creates a ResourcesCapabilities from GLib.Variant.
  244. *
  245. * @param variant The GLib.Variant to deserialize
  246. * @return The deserialized ResourcesCapabilities
  247. * @throws Error If deserialization fails
  248. */
  249. public ResourcesCapabilities.from_variant (Variant variant) throws Error {
  250. if (!variant.is_of_type (VariantType.VARDICT)) {
  251. throw new Mcp.Core.McpError.PARSE_ERROR ("ResourcesCapabilities must be a dictionary");
  252. }
  253. if (variant.lookup_value ("subscribe", null) != null) {
  254. this.subscribe = variant.lookup_value ("subscribe", VariantType.BOOLEAN).get_boolean ();
  255. }
  256. if (variant.lookup_value ("listChanged", null) != null) {
  257. this.list_changed = variant.lookup_value ("listChanged", VariantType.BOOLEAN).get_boolean ();
  258. }
  259. }
  260. }
  261. /**
  262. * Tools capabilities.
  263. */
  264. public class ToolsCapabilities : GLib.Object {
  265. /**
  266. * Whether the list of tools can change.
  267. */
  268. public bool list_changed { get; set; default = false; }
  269. /**
  270. * Creates a new ToolsCapabilities.
  271. */
  272. public ToolsCapabilities () {
  273. }
  274. /**
  275. * Serializes ToolsCapabilities to GLib.Variant.
  276. *
  277. * @return A GLib.Variant representing this ToolsCapabilities
  278. */
  279. public Variant to_variant () {
  280. var builder = Mcp.Types.VariantUtils.new_dict_builder ();
  281. builder.add ("{sv}", "listChanged", new Variant.boolean (list_changed));
  282. return builder.end ();
  283. }
  284. /**
  285. * Creates a ToolsCapabilities from GLib.Variant.
  286. *
  287. * @param variant The GLib.Variant to deserialize
  288. * @return The deserialized ToolsCapabilities
  289. * @throws Error If deserialization fails
  290. */
  291. public ToolsCapabilities.from_variant (Variant variant) throws Error {
  292. if (!variant.is_of_type (VariantType.VARDICT)) {
  293. throw new Mcp.Core.McpError.PARSE_ERROR ("ToolsCapabilities must be a dictionary");
  294. }
  295. if (variant.lookup_value ("listChanged", null) != null) {
  296. this.list_changed = variant.lookup_value ("listChanged", VariantType.BOOLEAN).get_boolean ();
  297. }
  298. }
  299. }
  300. }