-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdraft.rs
More file actions
1809 lines (1694 loc) · 89.1 KB
/
Copy pathdraft.rs
File metadata and controls
1809 lines (1694 loc) · 89.1 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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// # Solana Protocol for Agent and MCP Server Registries
///
/// This program implements two interconnected Solana-based registry protocols:
///
/// 1. **Agent Registry**: A decentralized directory for autonomous agents operating on Solana,
/// supporting the advertisement of agent capabilities, endpoints, identity, and metadata
/// following the Autonomous Economic Agent (AEA) and Agent-to-Agent (A2A) paradigms.
///
/// 2. **MCP Server Registry**: A directory for Model Context Protocol (MCP) compliant servers,
/// enabling the discovery of AI tools, resources, and prompts following the MCP specification.
///
/// Both registries use a hybrid storage model where essential verifiable data resides on-chain,
/// while more extensive metadata is stored off-chain (e.g., on IPFS/Arweave) and linked via URIs,
/// with on-chain hashes ensuring data integrity.
///
/// The registries emit detailed events designed to power off-chain indexing and query services
/// for advanced discovery capabilities.
use anchor_lang::prelude::*;
use anchor_lang::solana_program::system_program;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"); // Replace with your program ID after deployment
/// Constants defining size limits and PDA seeds for the registry protocols
mod constants {
/// ## Agent Registry Constants
/// Maximum allowed length for agent identifiers
pub const MAX_AGENT_ID_LEN: usize = 64;
/// Maximum allowed length for agent names
pub const MAX_AGENT_NAME_LEN: usize = 128;
/// Maximum allowed length for agent descriptions
pub const MAX_AGENT_DESCRIPTION_LEN: usize = 512;
/// Maximum allowed length for agent version strings
pub const MAX_AGENT_VERSION_LEN: usize = 32;
/// Maximum allowed length for provider names
pub const MAX_PROVIDER_NAME_LEN: usize = 128;
/// Maximum allowed length for provider URLs
pub const MAX_PROVIDER_URL_LEN: usize = 256;
/// Maximum allowed length for documentation URLs (shared by both registries)
pub const MAX_DOCUMENTATION_URL_LEN: usize = 256;
/// Maximum number of service endpoints an agent can register on-chain
pub const MAX_SERVICE_ENDPOINTS: usize = 3;
/// Maximum allowed length for endpoint protocol identifiers
pub const MAX_ENDPOINT_PROTOCOL_LEN: usize = 64;
/// Maximum allowed length for endpoint URLs
pub const MAX_ENDPOINT_URL_LEN: usize = 256;
/// Maximum number of supported input/output modes
pub const MAX_SUPPORTED_MODES: usize = 5;
/// Maximum allowed length for mode identifiers
pub const MAX_MODE_LEN: usize = 64;
/// Maximum number of skills an agent can register on-chain
pub const MAX_SKILLS: usize = 10;
/// Maximum allowed length for skill identifiers
pub const MAX_SKILL_ID_LEN: usize = 64;
/// Maximum allowed length for skill names
pub const MAX_SKILL_NAME_LEN: usize = 128;
/// Maximum number of tags per skill
pub const MAX_SKILL_TAGS: usize = 5;
/// Maximum allowed length for skill tags
pub const MAX_SKILL_TAG_LEN: usize = 32;
/// Maximum allowed length for security information URIs
pub const MAX_SECURITY_INFO_URI_LEN: usize = 256;
/// Maximum allowed length for Autonomous Economic Agent (AEA) addresses
pub const MAX_AEA_ADDRESS_LEN: usize = 128;
/// Maximum allowed length for economic intent summaries
pub const MAX_ECONOMIC_INTENT_LEN: usize = 256;
/// Maximum allowed length for extended metadata URIs
pub const MAX_EXTENDED_METADATA_URI_LEN: usize = 256;
/// Maximum number of tags per agent
pub const MAX_AGENT_TAGS: usize = 10;
/// Maximum allowed length for agent tags
pub const MAX_AGENT_TAG_LEN: usize = 32;
/// ## MCP Server Registry Constants
/// Maximum allowed length for MCP server identifiers
pub const MAX_SERVER_ID_LEN: usize = 64;
/// Maximum allowed length for server names
pub const MAX_SERVER_NAME_LEN: usize = 128;
/// Maximum allowed length for server version strings
pub const MAX_SERVER_VERSION_LEN: usize = 32;
/// Maximum allowed length for server endpoint URLs
pub const MAX_SERVER_ENDPOINT_URL_LEN: usize = 256;
/// Maximum allowed length for server capabilities summaries
pub const MAX_SERVER_CAPABILITIES_SUMMARY_LEN: usize = 256;
/// Maximum number of on-chain tool definitions per MCP server
pub const MAX_ONCHAIN_TOOL_DEFINITIONS: usize = 5;
/// Maximum allowed length for tool names
pub const MAX_TOOL_NAME_LEN: usize = 64;
/// Maximum number of tags per tool
pub const MAX_TOOL_TAGS: usize = 3;
/// Maximum allowed length for tool tags
pub const MAX_TOOL_TAG_LEN: usize = 32;
/// Maximum number of on-chain resource definitions per MCP server
pub const MAX_ONCHAIN_RESOURCE_DEFINITIONS: usize = 5;
/// Maximum allowed length for resource URI patterns
pub const MAX_RESOURCE_URI_PATTERN_LEN: usize = 128;
/// Maximum number of tags per resource
pub const MAX_RESOURCE_TAGS: usize = 3;
/// Maximum allowed length for resource tags
pub const MAX_RESOURCE_TAG_LEN: usize = 32;
/// Maximum number of on-chain prompt definitions per MCP server
pub const MAX_ONCHAIN_PROMPT_DEFINITIONS: usize = 5;
/// Maximum allowed length for prompt names
pub const MAX_PROMPT_NAME_LEN: usize = 64;
/// Maximum number of tags per prompt
pub const MAX_PROMPT_TAGS: usize = 3;
/// Maximum allowed length for prompt tags
pub const MAX_PROMPT_TAG_LEN: usize = 32;
/// Maximum allowed length for full capabilities URIs
pub const MAX_FULL_CAPABILITIES_URI_LEN: usize = 256;
/// Maximum number of tags per MCP server
pub const MAX_SERVER_TAGS: usize = 10;
/// Maximum allowed length for server tags
pub const MAX_SERVER_TAG_LEN: usize = 32;
/// Size of SHA256 hash in bytes
pub const HASH_SIZE: usize = 32; // SHA256 hash size
/// Size of String/Vec length prefix in Borsh serialization
pub const STRING_LEN_PREFIX_SIZE: usize = 4; // u32 for String/Vec length
/// Size of Option discriminator in Borsh serialization
pub const OPTION_DISCRIMINATOR_SIZE: usize = 1; // 1 byte for Option discriminator
/// ## PDA Seed Prefixes
/// Seed prefix used to derive Agent Registry PDAs
pub const AGENT_REGISTRY_PDA_SEED: &[u8] = b"agent_reg_v1";
/// Seed prefix used to derive MCP Server Registry PDAs
pub const MCP_SERVER_REGISTRY_PDA_SEED: &[u8] = b"mcp_srv_reg_v1";
}
use constants::*;
/// Calculate the Borsh serialization size of a String with a maximum length
///
/// Returns the size in bytes that a String will occupy when serialized with Borsh,
/// accounting for the length prefix and the maximum possible string content.
fn borsh_size_string(max_len: usize) -> usize {
STRING_LEN_PREFIX_SIZE + max_len
}
/// Calculate the Borsh serialization size of an Option<String> with a maximum length
///
/// Returns the size in bytes that an Option<String> will occupy when serialized with Borsh,
/// accounting for the discriminator byte, length prefix, and the maximum possible string content.
fn borsh_size_option_string(max_len: usize) -> usize {
OPTION_DISCRIMINATOR_SIZE + borsh_size_string(max_len)
}
/// Calculate the Borsh serialization size of an Option<[u8; HASH_SIZE]>
///
/// Returns the size in bytes that an Option<[u8; HASH_SIZE]> will occupy when serialized with Borsh,
/// accounting for the discriminator byte and the fixed-size hash.
fn borsh_size_option_hash() -> usize {
OPTION_DISCRIMINATOR_SIZE + HASH_SIZE
}
/// Calculate the Borsh serialization size of a Vec<String> with a maximum number of items and maximum item length
///
/// Returns the size in bytes that a Vec<String> will occupy when serialized with Borsh,
/// accounting for the length prefix and the maximum possible number of strings each with their maximum length.
fn borsh_size_vec_string(max_items: usize, max_item_len: usize) -> usize {
STRING_LEN_PREFIX_SIZE + (max_items * borsh_size_string(max_item_len))
}
/// Calculate the Borsh serialization size of a Vec<T> where T implements AccountSize
///
/// Returns the size in bytes that a Vec<T> will occupy when serialized with Borsh,
/// accounting for the length prefix and the maximum possible number of items each with their fixed size.
fn borsh_size_vec_struct<T: AccountSize>(max_items: usize) -> usize {
STRING_LEN_PREFIX_SIZE + (max_items * T::ACCOUNT_SIZE)
}
/// Trait for determining the fixed size of a struct when serialized with Borsh
///
/// Used to calculate the space requirements for storing collections of structs in accounts.
trait AccountSize { // Trait to get struct size for Vec calculations
/// The size in bytes that this type occupies when serialized with Borsh
const ACCOUNT_SIZE: usize;
}
/// # Solana AI Registries Program
///
/// This program module implements the core functionality for both the Agent Registry and
/// the MCP Server Registry protocols. It provides instructions for registering, updating,
/// and managing entities in each registry.
#[program]
pub mod solana_ai_registries {
use super::*;
// --- Agent Registry Instructions ---
/// Register a new agent in the Agent Registry
///
/// This instruction initializes a new PDA account for the agent and populates it with
/// the provided details. The PDA is derived using the agent_id as a seed.
/// The payer of the transaction funds the new PDA account with enough lamports to make
/// it rent-exempt.
///
/// Emits an `AgentRegistered` event containing the full agent data.
///
/// # Parameters
/// * `ctx` - The context containing accounts involved in the instruction
/// * `agent_id` - Unique identifier for the agent
/// * `name` - Human-readable name of the agent
/// * `description` - Human-readable description of the agent
/// * `agent_version` - Version of the agent software/implementation
/// * `provider_name` - Optional name of the agent's provider organization
/// * `provider_url` - Optional URL of the agent's provider
/// * `documentation_url` - Optional URL to human-readable documentation
/// * `service_endpoints_input` - List of service endpoints where the agent can be reached
/// * `capabilities_flags` - Bitmask for core A2A capabilities
/// * `supported_input_modes_input` - Default accepted input MIME types
/// * `supported_output_modes_input` - Default produced output MIME types
/// * `skills_input` - Summary of key agent skills
/// * `security_info_uri` - Optional URI to detailed security scheme definitions
/// * `aea_address` - Optional Fetch.ai AEA address/ID
/// * `economic_intent_summary` - Optional brief summary of agent's economic goals
/// * `supported_aea_protocols_hash` - Optional SHA256 hash of supported AEA protocol IDs
/// * `extended_metadata_uri` - Optional URI to extensive off-chain metadata
/// * `tags_input` - General discoverability tags for the agent
///
/// # Returns
/// * Result indicating success or providing error details
pub fn register_agent(
ctx: Context<RegisterAgent>,
agent_id: String,
name: String,
description: String,
agent_version: String,
provider_name: Option<String>,
provider_url: Option<String>,
documentation_url: Option<String>,
service_endpoints_input: Vec<ServiceEndpointInput>,
capabilities_flags: u64,
supported_input_modes_input: Vec<String>,
supported_output_modes_input: Vec<String>,
skills_input: Vec<AgentSkillInput>,
security_info_uri: Option<String>,
aea_address: Option<String>,
economic_intent_summary: Option<String>,
supported_aea_protocols_hash: Option<[u8; HASH_SIZE]>,
extended_metadata_uri: Option<String>,
tags_input: Vec<String>,
) -> Result<()> {
// Exhaustive Input validations
if agent_id.len() > MAX_AGENT_ID_LEN || agent_id.is_empty() { return err!(ErrorCode::InvalidAgentIdLength); }
if name.len() > MAX_AGENT_NAME_LEN || name.is_empty() { return err!(ErrorCode::InvalidNameLength); }
if description.len() > MAX_AGENT_DESCRIPTION_LEN { return err!(ErrorCode::InvalidDescriptionLength); } // Can be empty
if agent_version.len() > MAX_AGENT_VERSION_LEN || agent_version.is_empty() { return err!(ErrorCode::InvalidVersionLength); }
if let Some(ref val) = provider_name { if val.len() > MAX_PROVIDER_NAME_LEN { return err!(ErrorCode::InvalidProviderNameLength); }}
if let Some(ref val) = provider_url { if val.len() > MAX_PROVIDER_URL_LEN { return err!(ErrorCode::InvalidProviderUrlLength); }}
if let Some(ref val) = documentation_url { if val.len() > MAX_DOCUMENTATION_URL_LEN { return err!(ErrorCode::InvalidDocumentationUrlLength); }}
if service_endpoints_input.len() > MAX_SERVICE_ENDPOINTS { return err!(ErrorCode::TooManyServiceEndpoints); }
for se in &service_endpoints_input {
if se.protocol.len() > MAX_ENDPOINT_PROTOCOL_LEN || se.protocol.is_empty() { return err!(ErrorCode::InvalidEndpointProtocolLength); }
if se.url.len() > MAX_ENDPOINT_URL_LEN || se.url.is_empty() { return err!(ErrorCode::InvalidEndpointUrlLength); }
}
if supported_input_modes_input.len() > MAX_SUPPORTED_MODES { return err!(ErrorCode::TooManySupportedModes); }
for mode in &supported_input_modes_input { if mode.len() > MAX_MODE_LEN || mode.is_empty() { return err!(ErrorCode::InvalidModeLength); }}
if supported_output_modes_input.len() > MAX_SUPPORTED_MODES { return err!(ErrorCode::TooManySupportedModes); }
for mode in &supported_output_modes_input { if mode.len() > MAX_MODE_LEN || mode.is_empty() { return err!(ErrorCode::InvalidModeLength); }}
if skills_input.len() > MAX_SKILLS { return err!(ErrorCode::TooManySkills); }
for skill in &skills_input {
if skill.id.len() > MAX_SKILL_ID_LEN || skill.id.is_empty() { return err!(ErrorCode::InvalidSkillIdLength); }
if skill.name.len() > MAX_SKILL_NAME_LEN || skill.name.is_empty() { return err!(ErrorCode::InvalidSkillNameLength); }
if skill.tags.len() > MAX_SKILL_TAGS { return err!(ErrorCode::TooManySkillTags); }
for tag in &skill.tags { if tag.len() > MAX_SKILL_TAG_LEN || tag.is_empty() { return err!(ErrorCode::InvalidSkillTagLength); }}
}
if let Some(ref val) = security_info_uri { if val.len() > MAX_SECURITY_INFO_URI_LEN { return err!(ErrorCode::InvalidSecurityInfoUriLength); }}
if let Some(ref val) = aea_address { if val.len() > MAX_AEA_ADDRESS_LEN { return err!(ErrorCode::InvalidAeaAddressLength); }}
if let Some(ref val) = economic_intent_summary { if val.len() > MAX_ECONOMIC_INTENT_LEN { return err!(ErrorCode::InvalidEconomicIntentLength); }}
if let Some(ref val) = extended_metadata_uri { if val.len() > MAX_EXTENDED_METADATA_URI_LEN { return err!(ErrorCode::InvalidExtendedMetadataUriLength); }}
if tags_input.len() > MAX_AGENT_TAGS { return err!(ErrorCode::TooManyAgentTags); }
for tag in &tags_input { if tag.len() > MAX_AGENT_TAG_LEN || tag.is_empty() { return err!(ErrorCode::InvalidAgentTagLength); }}
let agent_entry = &mut ctx.accounts.agent_entry;
agent_entry.bump = *ctx.bumps.get("agent_entry").ok_or(ErrorCode::BumpSeedNotInHashMap)?;
agent_entry.registry_version = 1;
agent_entry.owner_authority = ctx.accounts.owner_authority.key();
agent_entry.agent_id = agent_id.clone();
agent_entry.name = name;
agent_entry.description = description;
agent_entry.agent_version = agent_version;
agent_entry.provider_name = provider_name;
agent_entry.provider_url = provider_url;
agent_entry.documentation_url = documentation_url;
agent_entry.service_endpoints = service_endpoints_input.into_iter().map(|sei| sei.into()).collect();
let default_count = agent_entry.service_endpoints.iter().filter(|se| se.is_default).count();
if default_count > 1 { return err!(ErrorCode::MultipleDefaultEndpoints); }
if default_count == 0 && !agent_entry.service_endpoints.is_empty() { return err!(ErrorCode::MissingDefaultEndpoint); }
agent_entry.capabilities_flags = capabilities_flags;
agent_entry.supported_input_modes = supported_input_modes_input;
agent_entry.supported_output_modes = supported_output_modes_input;
agent_entry.skills = skills_input.into_iter().map(|asi| asi.into()).collect();
agent_entry.security_info_uri = security_info_uri;
agent_entry.aea_address = aea_address;
agent_entry.economic_intent_summary = economic_intent_summary;
agent_entry.supported_aea_protocols_hash = supported_aea_protocols_hash;
agent_entry.status = AgentStatus::Pending as u8;
agent_entry.registration_timestamp = Clock::get()?.unix_timestamp;
agent_entry.last_update_timestamp = Clock::get()?.unix_timestamp;
agent_entry.extended_metadata_uri = extended_metadata_uri;
agent_entry.tags = tags_input;
emit!(AgentRegistered {
registry_version: agent_entry.registry_version,
owner_authority: agent_entry.owner_authority,
agent_id: agent_entry.agent_id.clone(),
name: agent_entry.name.clone(),
description: agent_entry.description.clone(),
agent_version: agent_entry.agent_version.clone(),
provider_name: agent_entry.provider_name.clone(),
provider_url: agent_entry.provider_url.clone(),
documentation_url: agent_entry.documentation_url.clone(),
service_endpoints: agent_entry.service_endpoints.clone(),
capabilities_flags: agent_entry.capabilities_flags,
supported_input_modes: agent_entry.supported_input_modes.clone(),
supported_output_modes: agent_entry.supported_output_modes.clone(),
skills: agent_entry.skills.clone(),
security_info_uri: agent_entry.security_info_uri.clone(),
aea_address: agent_entry.aea_address.clone(),
economic_intent_summary: agent_entry.economic_intent_summary.clone(),
supported_aea_protocols_hash: agent_entry.supported_aea_protocols_hash,
status: agent_entry.status,
registration_timestamp: agent_entry.registration_timestamp,
last_update_timestamp: agent_entry.last_update_timestamp,
extended_metadata_uri: agent_entry.extended_metadata_uri.clone(),
tags: agent_entry.tags.clone(),
});
Ok(())
}
/// Update the details of an existing agent in the Agent Registry
///
/// Allows the owner_authority to modify mutable fields of an existing agent entry.
/// Only the fields provided in the input will be updated; other fields remain unchanged.
/// The function performs validation on each provided field to ensure data integrity.
///
/// Emits an `AgentUpdated` event, detailing the agent_id and the fields that were changed.
///
/// # Parameters
/// * `ctx` - The context containing accounts involved in the instruction
/// * `details` - A struct containing optional fields to update; only provided fields will be changed
///
/// # Returns
/// * Result indicating success or providing error details
pub fn update_agent_details(ctx: Context<UpdateAgent>, details: AgentUpdateDetailsInput) -> Result<()> {
let agent_entry = &mut ctx.accounts.agent_entry;
let mut changed_fields: Vec<String> = Vec::new();
if let Some(val) = details.name {
if val.len() > MAX_AGENT_NAME_LEN || val.is_empty() { return err!(ErrorCode::InvalidNameLength); }
agent_entry.name = val; changed_fields.push("name".to_string());
}
if let Some(val) = details.description { // Description can be empty, but not exceed max length
if val.len() > MAX_AGENT_DESCRIPTION_LEN { return err!(ErrorCode::InvalidDescriptionLength); }
agent_entry.description = val; changed_fields.push("description".to_string());
}
if let Some(val) = details.agent_version {
if val.len() > MAX_AGENT_VERSION_LEN || val.is_empty() { return err!(ErrorCode::InvalidVersionLength); }
agent_entry.agent_version = val; changed_fields.push("agent_version".to_string());
}
macro_rules! update_optional_string_field {
($field_name:ident, $max_len:ident, $error_code:ident, $clear_flag:ident) => {
if let Some(val) = details.$field_name {
if val.len() > $max_len { return err!(ErrorCode::$error_code); }
// Allow empty strings for optional fields if not explicitly disallowed by protocol for that field
agent_entry.$field_name = Some(val); changed_fields.push(stringify!($field_name).to_string());
} else if details.$clear_flag.unwrap_or(false) {
agent_entry.$field_name = None; changed_fields.push(stringify!($field_name).to_string());
}
};
}
update_optional_string_field!(provider_name, MAX_PROVIDER_NAME_LEN, InvalidProviderNameLength, clear_provider_name);
update_optional_string_field!(provider_url, MAX_PROVIDER_URL_LEN, InvalidProviderUrlLength, clear_provider_url);
update_optional_string_field!(documentation_url, MAX_DOCUMENTATION_URL_LEN, InvalidDocumentationUrlLength, clear_documentation_url);
update_optional_string_field!(security_info_uri, MAX_SECURITY_INFO_URI_LEN, InvalidSecurityInfoUriLength, clear_security_info_uri);
update_optional_string_field!(aea_address, MAX_AEA_ADDRESS_LEN, InvalidAeaAddressLength, clear_aea_address);
update_optional_string_field!(economic_intent_summary, MAX_ECONOMIC_INTENT_LEN, InvalidEconomicIntentLength, clear_economic_intent_summary);
update_optional_string_field!(extended_metadata_uri, MAX_EXTENDED_METADATA_URI_LEN, InvalidExtendedMetadataUriLength, clear_extended_metadata_uri);
if let Some(val) = details.service_endpoints {
if val.len() > MAX_SERVICE_ENDPOINTS { return err!(ErrorCode::TooManyServiceEndpoints); }
for se in &val {
if se.protocol.len() > MAX_ENDPOINT_PROTOCOL_LEN || se.protocol.is_empty() { return err!(ErrorCode::InvalidEndpointProtocolLength); }
if se.url.len() > MAX_ENDPOINT_URL_LEN || se.url.is_empty() { return err!(ErrorCode::InvalidEndpointUrlLength); }
}
let mapped_endpoints: Vec<ServiceEndpoint> = val.into_iter().map(|sei| sei.into()).collect();
let default_count = mapped_endpoints.iter().filter(|se| se.is_default).count();
if default_count > 1 { return err!(ErrorCode::MultipleDefaultEndpoints); }
if default_count == 0 && !mapped_endpoints.is_empty() { return err!(ErrorCode::MissingDefaultEndpoint); }
agent_entry.service_endpoints = mapped_endpoints; changed_fields.push("service_endpoints".to_string());
}
if let Some(val) = details.capabilities_flags { agent_entry.capabilities_flags = val; changed_fields.push("capabilities_flags".to_string());}
if let Some(val) = details.supported_input_modes {
if val.len() > MAX_SUPPORTED_MODES { return err!(ErrorCode::TooManySupportedModes); }
for mode in &val { if mode.len() > MAX_MODE_LEN || mode.is_empty() { return err!(ErrorCode::InvalidModeLength); }}
agent_entry.supported_input_modes = val; changed_fields.push("supported_input_modes".to_string());
}
if let Some(val) = details.supported_output_modes {
if val.len() > MAX_SUPPORTED_MODES { return err!(ErrorCode::TooManySupportedModes); }
for mode in &val { if mode.len() > MAX_MODE_LEN || mode.is_empty() { return err!(ErrorCode::InvalidModeLength); }}
agent_entry.supported_output_modes = val; changed_fields.push("supported_output_modes".to_string());
}
if let Some(val) = details.skills {
if val.len() > MAX_SKILLS { return err!(ErrorCode::TooManySkills); }
for skill in &val {
if skill.id.len() > MAX_SKILL_ID_LEN || skill.id.is_empty() { return err!(ErrorCode::InvalidSkillIdLength); }
if skill.name.len() > MAX_SKILL_NAME_LEN || skill.name.is_empty() { return err!(ErrorCode::InvalidSkillNameLength); }
if skill.tags.len() > MAX_SKILL_TAGS { return err!(ErrorCode::TooManySkillTags); }
for tag in &skill.tags { if tag.len() > MAX_SKILL_TAG_LEN || tag.is_empty() { return err!(ErrorCode::InvalidSkillTagLength); }}
}
agent_entry.skills = val.into_iter().map(|asi| asi.into()).collect(); changed_fields.push("skills".to_string());
}
if let Some(val) = details.supported_aea_protocols_hash {
agent_entry.supported_aea_protocols_hash = Some(val); changed_fields.push("supported_aea_protocols_hash".to_string());
} else if details.clear_supported_aea_protocols_hash.unwrap_or(false) {
agent_entry.supported_aea_protocols_hash = None; changed_fields.push("supported_aea_protocols_hash".to_string());
}
if let Some(val) = details.tags {
if val.len() > MAX_AGENT_TAGS { return err!(ErrorCode::TooManyAgentTags); }
for tag in &val { if tag.len() > MAX_AGENT_TAG_LEN || tag.is_empty() { return err!(ErrorCode::InvalidAgentTagLength); }}
agent_entry.tags = val; changed_fields.push("tags".to_string());
}
if changed_fields.is_empty() { return Ok(()); }
agent_entry.last_update_timestamp = Clock::get()?.unix_timestamp;
emit!(AgentUpdated {
agent_id: agent_entry.agent_id.clone(),
changed_fields: changed_fields,
last_update_timestamp: agent_entry.last_update_timestamp,
});
Ok(())
}
/// Update the status of an existing agent in the Agent Registry
///
/// A specialized instruction for changing the agent's operational status
/// (Pending, Active, Inactive, Deregistered).
/// Requires owner_authority signature for authorization.
///
/// Emits an `AgentStatusChanged` event with agent_id and the new_status.
///
/// # Parameters
/// * `ctx` - The context containing accounts involved in the instruction
/// * `new_status_val` - The new status value (0: Pending, 1: Active, 2: Inactive, 3: Deregistered)
///
/// # Returns
/// * Result indicating success or providing error details
pub fn update_agent_status(ctx: Context<UpdateAgent>, new_status_val: u8) -> Result<()> {
let agent_entry = &mut ctx.accounts.agent_entry;
let new_status = AgentStatus::from_u8(new_status_val).ok_or(ErrorCode::InvalidAgentStatus)?;
if agent_entry.status == new_status as u8 { return Ok(()); }
agent_entry.status = new_status as u8;
agent_entry.last_update_timestamp = Clock::get()?.unix_timestamp;
emit!(AgentStatusChanged {
agent_id: agent_entry.agent_id.clone(),
new_status: agent_entry.status,
last_update_timestamp: agent_entry.last_update_timestamp,
});
Ok(())
}
/// Deregister an agent from the Agent Registry
///
/// Allows the owner_authority to mark an agent as deregistered.
/// This changes the agent's status to Deregistered, preserving its history
/// rather than completely removing the entry.
///
/// Emits an `AgentDeregistered` event with agent_id and deregistration timestamp.
///
/// # Parameters
/// * `ctx` - The context containing accounts involved in the instruction
///
/// # Returns
/// * Result indicating success or providing error details
pub fn deregister_agent(ctx: Context<UpdateAgent>) -> Result<()> {
let agent_entry = &mut ctx.accounts.agent_entry;
if agent_entry.status == AgentStatus::Deregistered as u8 { return Ok(()); }
agent_entry.status = AgentStatus::Deregistered as u8;
agent_entry.last_update_timestamp = Clock::get()?.unix_timestamp;
emit!(AgentDeregistered {
agent_id: agent_entry.agent_id.clone(),
deregistration_timestamp: agent_entry.last_update_timestamp,
});
Ok(())
}
// --- MCP Server Registry Instructions ---
/// Register a new MCP server in the MCP Server Registry
///
/// This instruction initializes a new PDA account for the MCP server and populates it with
/// the provided details. The PDA is derived using the server_id as a seed.
/// The payer of the transaction funds the new PDA account with enough lamports to make
/// it rent-exempt.
///
/// MCP servers follow the Model Context Protocol specification, enabling AI applications
/// to discover and interact with external data sources and tools.
///
/// Emits an `McpServerRegistered` event containing the full server data.
///
/// # Parameters
/// * `ctx` - The context containing accounts involved in the instruction
/// * `server_id` - Unique identifier for the MCP server
/// * `name` - Human-readable name of the server
/// * `server_version` - Version of the MCP server software
/// * `service_endpoint` - Primary URL for MCP communication
/// * `documentation_url` - Optional URL to human-readable documentation
/// * `server_capabilities_summary` - Optional brief summary of server offerings
/// * `supports_resources` - Whether server offers MCP Resources
/// * `supports_tools` - Whether server offers MCP Tools
/// * `supports_prompts` - Whether server offers MCP Prompts
/// * `onchain_tool_definitions_input` - Summary of key on-chain advertised tools
/// * `onchain_resource_definitions_input` - Summary of key on-chain advertised resources
/// * `onchain_prompt_definitions_input` - Summary of key on-chain advertised prompts
/// * `full_capabilities_uri` - Optional URI to off-chain JSON with full tool/resource/prompt definitions
/// * `tags_input` - General discoverability tags for the server
///
/// # Returns
/// * Result indicating success or providing error details
pub fn register_mcp_server(
ctx: Context<RegisterMcpServer>,
server_id: String,
name: String,
server_version: String,
service_endpoint: String,
documentation_url: Option<String>,
server_capabilities_summary: Option<String>,
supports_resources: bool,
supports_tools: bool,
supports_prompts: bool,
onchain_tool_definitions_input: Vec<McpToolDefinitionOnChainInput>,
onchain_resource_definitions_input: Vec<McpResourceDefinitionOnChainInput>,
onchain_prompt_definitions_input: Vec<McpPromptDefinitionOnChainInput>,
full_capabilities_uri: Option<String>,
tags_input: Vec<String>,
) -> Result<()> {
// Exhaustive Input validations
if server_id.len() > MAX_SERVER_ID_LEN || server_id.is_empty() { return err!(ErrorCode::InvalidServerIdLength); }
if name.len() > MAX_SERVER_NAME_LEN || name.is_empty() { return err!(ErrorCode::InvalidNameLength); }
if server_version.len() > MAX_SERVER_VERSION_LEN || server_version.is_empty() { return err!(ErrorCode::InvalidVersionLength); }
if service_endpoint.len() > MAX_SERVER_ENDPOINT_URL_LEN || service_endpoint.is_empty() { return err!(ErrorCode::InvalidEndpointUrlLength); }
// Assuming HTTPS is a client-side or off-chain indexer validation for URIs, not strictly on-chain unless specified
if let Some(ref val) = documentation_url { if val.len() > MAX_DOCUMENTATION_URL_LEN { return err!(ErrorCode::InvalidDocumentationUrlLength); }}
if let Some(ref val) = server_capabilities_summary { if val.len() > MAX_SERVER_CAPABILITIES_SUMMARY_LEN { return err!(ErrorCode::InvalidServerCapabilitiesSummaryLength); }}
if onchain_tool_definitions_input.len() > MAX_ONCHAIN_TOOL_DEFINITIONS {return err!(ErrorCode::TooManyToolDefinitions); }
for tool_def in &onchain_tool_definitions_input {
if tool_def.name.len() > MAX_TOOL_NAME_LEN || tool_def.name.is_empty() { return err!(ErrorCode::InvalidToolNameLength); }
if tool_def.tags.len() > MAX_TOOL_TAGS { return err!(ErrorCode::TooManyToolTags); }
for tag in &tool_def.tags { if tag.len() > MAX_TOOL_TAG_LEN || tag.is_empty() { return err!(ErrorCode::InvalidToolTagLength); }}
}
if onchain_resource_definitions_input.len() > MAX_ONCHAIN_RESOURCE_DEFINITIONS {return err!(ErrorCode::TooManyResourceDefinitions); }
for res_def in &onchain_resource_definitions_input {
if res_def.uri_pattern.len() > MAX_RESOURCE_URI_PATTERN_LEN || res_def.uri_pattern.is_empty() { return err!(ErrorCode::InvalidResourceUriPatternLength); }
if res_def.tags.len() > MAX_RESOURCE_TAGS { return err!(ErrorCode::TooManyResourceTags); }
for tag in &res_def.tags { if tag.len() > MAX_RESOURCE_TAG_LEN || tag.is_empty() { return err!(ErrorCode::InvalidResourceTagLength); }}
}
if onchain_prompt_definitions_input.len() > MAX_ONCHAIN_PROMPT_DEFINITIONS {return err!(ErrorCode::TooManyPromptDefinitions); }
for prompt_def in &onchain_prompt_definitions_input {
if prompt_def.name.len() > MAX_PROMPT_NAME_LEN || prompt_def.name.is_empty() { return err!(ErrorCode::InvalidPromptNameLength); }
if prompt_def.tags.len() > MAX_PROMPT_TAGS { return err!(ErrorCode::TooManyPromptTags); }
for tag in &prompt_def.tags { if tag.len() > MAX_PROMPT_TAG_LEN || tag.is_empty() { return err!(ErrorCode::InvalidPromptTagLength); }}
}
if let Some(ref val) = full_capabilities_uri { if val.len() > MAX_FULL_CAPABILITIES_URI_LEN { return err!(ErrorCode::InvalidFullCapabilitiesUriLength); }}
if tags_input.len() > MAX_SERVER_TAGS { return err!(ErrorCode::TooManyServerTags); }
for tag in &tags_input { if tag.len() > MAX_SERVER_TAG_LEN || tag.is_empty() { return err!(ErrorCode::InvalidServerTagLength); }}
let mcp_server_entry = &mut ctx.accounts.mcp_server_entry;
mcp_server_entry.bump = *ctx.bumps.get("mcp_server_entry").ok_or(ErrorCode::BumpSeedNotInHashMap)?;
mcp_server_entry.registry_version = 1;
mcp_server_entry.owner_authority = ctx.accounts.owner_authority.key();
mcp_server_entry.server_id = server_id.clone();
mcp_server_entry.name = name;
mcp_server_entry.server_version = server_version;
mcp_server_entry.service_endpoint = service_endpoint;
mcp_server_entry.documentation_url = documentation_url;
mcp_server_entry.server_capabilities_summary = server_capabilities_summary;
mcp_server_entry.supports_resources = supports_resources;
mcp_server_entry.supports_tools = supports_tools;
mcp_server_entry.supports_prompts = supports_prompts;
mcp_server_entry.onchain_tool_definitions = onchain_tool_definitions_input.into_iter().map(|i| i.into()).collect();
mcp_server_entry.onchain_resource_definitions = onchain_resource_definitions_input.into_iter().map(|i| i.into()).collect();
mcp_server_entry.onchain_prompt_definitions = onchain_prompt_definitions_input.into_iter().map(|i| i.into()).collect();
mcp_server_entry.status = McpServerStatus::Pending as u8;
mcp_server_entry.registration_timestamp = Clock::get()?.unix_timestamp;
mcp_server_entry.last_update_timestamp = Clock::get()?.unix_timestamp;
mcp_server_entry.full_capabilities_uri = full_capabilities_uri;
mcp_server_entry.tags = tags_input;
emit!(McpServerRegistered {
registry_version: mcp_server_entry.registry_version,
owner_authority: mcp_server_entry.owner_authority,
server_id: mcp_server_entry.server_id.clone(),
name: mcp_server_entry.name.clone(),
server_version: mcp_server_entry.server_version.clone(),
service_endpoint: mcp_server_entry.service_endpoint.clone(),
documentation_url: mcp_server_entry.documentation_url.clone(),
server_capabilities_summary: mcp_server_entry.server_capabilities_summary.clone(),
supports_resources: mcp_server_entry.supports_resources,
supports_tools: mcp_server_entry.supports_tools,
supports_prompts: mcp_server_entry.supports_prompts,
onchain_tool_definitions: mcp_server_entry.onchain_tool_definitions.clone(),
onchain_resource_definitions: mcp_server_entry.onchain_resource_definitions.clone(),
onchain_prompt_definitions: mcp_server_entry.onchain_prompt_definitions.clone(),
status: mcp_server_entry.status,
registration_timestamp: mcp_server_entry.registration_timestamp,
last_update_timestamp: mcp_server_entry.last_update_timestamp,
full_capabilities_uri: mcp_server_entry.full_capabilities_uri.clone(),
tags: mcp_server_entry.tags.clone(),
});
Ok(())
}
/// Update the details of an existing MCP server in the MCP Server Registry
///
/// Allows the owner_authority to modify mutable fields of an existing MCP server entry.
/// Only the fields provided in the input will be updated; other fields remain unchanged.
/// The function performs validation on each provided field to ensure data integrity.
///
/// Emits an `McpServerUpdated` event, detailing the server_id and the fields that were changed.
///
/// # Parameters
/// * `ctx` - The context containing accounts involved in the instruction
/// * `details` - A struct containing optional fields to update; only provided fields will be changed
///
/// # Returns
/// * Result indicating success or providing error details
pub fn update_mcp_server_details(ctx: Context<UpdateMcpServer>, details: McpServerUpdateDetailsInput) -> Result<()> {
let mcp_server_entry = &mut ctx.accounts.mcp_server_entry;
let mut changed_fields: Vec<String> = Vec::new();
if let Some(val) = details.name {
if val.len() > MAX_SERVER_NAME_LEN || val.is_empty() { return err!(ErrorCode::InvalidNameLength); }
mcp_server_entry.name = val; changed_fields.push("name".to_string());
}
if let Some(val) = details.server_version {
if val.len() > MAX_SERVER_VERSION_LEN || val.is_empty() { return err!(ErrorCode::InvalidVersionLength); }
mcp_server_entry.server_version = val; changed_fields.push("server_version".to_string());
}
if let Some(val) = details.service_endpoint {
if val.len() > MAX_SERVER_ENDPOINT_URL_LEN || val.is_empty() { return err!(ErrorCode::InvalidEndpointUrlLength); }
mcp_server_entry.service_endpoint = val; changed_fields.push("service_endpoint".to_string());
}
macro_rules! update_mcp_optional_string_field {
($field_name:ident, $max_len:ident, $error_code:ident, $clear_flag:ident) => {
if let Some(val) = details.$field_name {
if val.len() > $max_len { return err!(ErrorCode::$error_code); }
mcp_server_entry.$field_name = Some(val); changed_fields.push(stringify!($field_name).to_string());
} else if details.$clear_flag.unwrap_or(false) {
mcp_server_entry.$field_name = None; changed_fields.push(stringify!($field_name).to_string());
}
};
}
update_mcp_optional_string_field!(documentation_url, MAX_DOCUMENTATION_URL_LEN, InvalidDocumentationUrlLength, clear_documentation_url);
update_mcp_optional_string_field!(server_capabilities_summary, MAX_SERVER_CAPABILITIES_SUMMARY_LEN, InvalidServerCapabilitiesSummaryLength, clear_server_capabilities_summary);
update_mcp_optional_string_field!(full_capabilities_uri, MAX_FULL_CAPABILITIES_URI_LEN, InvalidFullCapabilitiesUriLength, clear_full_capabilities_uri);
if let Some(val) = details.supports_resources { mcp_server_entry.supports_resources = val; changed_fields.push("supports_resources".to_string());}
if let Some(val) = details.supports_tools { mcp_server_entry.supports_tools = val; changed_fields.push("supports_tools".to_string());}
if let Some(val) = details.supports_prompts { mcp_server_entry.supports_prompts = val; changed_fields.push("supports_prompts".to_string());}
if let Some(val) = details.onchain_tool_definitions {
if val.len() > MAX_ONCHAIN_TOOL_DEFINITIONS {return err!(ErrorCode::TooManyToolDefinitions); }
for tool_def in &val {
if tool_def.name.len() > MAX_TOOL_NAME_LEN || tool_def.name.is_empty() { return err!(ErrorCode::InvalidToolNameLength); }
if tool_def.tags.len() > MAX_TOOL_TAGS { return err!(ErrorCode::TooManyToolTags); }
for tag in &tool_def.tags { if tag.len() > MAX_TOOL_TAG_LEN || tag.is_empty() { return err!(ErrorCode::InvalidToolTagLength); }}
}
mcp_server_entry.onchain_tool_definitions = val.into_iter().map(|i| i.into()).collect(); changed_fields.push("onchain_tool_definitions".to_string());
}
if let Some(val) = details.onchain_resource_definitions {
if val.len() > MAX_ONCHAIN_RESOURCE_DEFINITIONS {return err!(ErrorCode::TooManyResourceDefinitions); }
for res_def in &val {
if res_def.uri_pattern.len() > MAX_RESOURCE_URI_PATTERN_LEN || res_def.uri_pattern.is_empty() { return err!(ErrorCode::InvalidResourceUriPatternLength); }
if res_def.tags.len() > MAX_RESOURCE_TAGS { return err!(ErrorCode::TooManyResourceTags); }
for tag in &res_def.tags { if tag.len() > MAX_RESOURCE_TAG_LEN || tag.is_empty() { return err!(ErrorCode::InvalidResourceTagLength); }}
}
mcp_server_entry.onchain_resource_definitions = val.into_iter().map(|i| i.into()).collect(); changed_fields.push("onchain_resource_definitions".to_string());
}
if let Some(val) = details.onchain_prompt_definitions {
if val.len() > MAX_ONCHAIN_PROMPT_DEFINITIONS {return err!(ErrorCode::TooManyPromptDefinitions); }
for prompt_def in &val {
if prompt_def.name.len() > MAX_PROMPT_NAME_LEN || prompt_def.name.is_empty() { return err!(ErrorCode::InvalidPromptNameLength); }
if prompt_def.tags.len() > MAX_PROMPT_TAGS { return err!(ErrorCode::TooManyPromptTags); }
for tag in &prompt_def.tags { if tag.len() > MAX_PROMPT_TAG_LEN || tag.is_empty() { return err!(ErrorCode::InvalidPromptTagLength); }}
}
mcp_server_entry.onchain_prompt_definitions = val.into_iter().map(|i| i.into()).collect(); changed_fields.push("onchain_prompt_definitions".to_string());
}
if let Some(val) = details.tags {
if val.len() > MAX_SERVER_TAGS { return err!(ErrorCode::TooManyServerTags); }
for tag in &val { if tag.len() > MAX_SERVER_TAG_LEN || tag.is_empty() { return err!(ErrorCode::InvalidServerTagLength); }}
mcp_server_entry.tags = val; changed_fields.push("tags".to_string());
}
if changed_fields.is_empty() { return Ok(()); }
mcp_server_entry.last_update_timestamp = Clock::get()?.unix_timestamp;
emit!(McpServerUpdated {
server_id: mcp_server_entry.server_id.clone(),
changed_fields: changed_fields,
last_update_timestamp: mcp_server_entry.last_update_timestamp,
});
Ok(())
}
/// Update the status of an existing MCP server in the MCP Server Registry
///
/// A specialized instruction for changing the MCP server's operational status
/// (Pending, Active, Inactive, Deregistered).
/// Requires owner_authority signature for authorization.
///
/// Emits an `McpServerStatusChanged` event with server_id and the new_status.
///
/// # Parameters
/// * `ctx` - The context containing accounts involved in the instruction
/// * `new_status_val` - The new status value (0: Pending, 1: Active, 2: Inactive, 3: Deregistered)
///
/// # Returns
/// * Result indicating success or providing error details
pub fn update_mcp_server_status(ctx: Context<UpdateMcpServer>, new_status_val: u8) -> Result<()> {
let mcp_server_entry = &mut ctx.accounts.mcp_server_entry;
let new_status = McpServerStatus::from_u8(new_status_val).ok_or(ErrorCode::InvalidMcpServerStatus)?;
if mcp_server_entry.status == new_status as u8 { return Ok(()); }
mcp_server_entry.status = new_status as u8;
mcp_server_entry.last_update_timestamp = Clock::get()?.unix_timestamp;
emit!(McpServerStatusChanged {
server_id: mcp_server_entry.server_id.clone(),
new_status: mcp_server_entry.status,
last_update_timestamp: mcp_server_entry.last_update_timestamp,
});
Ok(())
}
/// Deregister an MCP server from the MCP Server Registry
///
/// Allows the owner_authority to mark an MCP server as deregistered.
/// This changes the server's status to Deregistered, preserving its history
/// rather than completely removing the entry.
///
/// Emits an `McpServerDeregistered` event with server_id and deregistration timestamp.
///
/// # Parameters
/// * `ctx` - The context containing accounts involved in the instruction
///
/// # Returns
/// * Result indicating success or providing error details
pub fn deregister_mcp_server(ctx: Context<UpdateMcpServer>) -> Result<()> {
let mcp_server_entry = &mut ctx.accounts.mcp_server_entry;
if mcp_server_entry.status == McpServerStatus::Deregistered as u8 { return Ok(()); }
mcp_server_entry.status = McpServerStatus::Deregistered as u8;
mcp_server_entry.last_update_timestamp = Clock::get()?.unix_timestamp;
emit!(McpServerDeregistered {
server_id: mcp_server_entry.server_id.clone(),
deregistration_timestamp: mcp_server_entry.last_update_timestamp,
});
Ok(())
}
}
// --- Account Context Structs ---
/// Accounts required for the register_agent instruction
///
/// This struct defines the accounts that must be provided when calling the register_agent instruction.
/// It creates a new PDA for the agent entry, using the agent_id as a seed.
#[derive(Accounts)]
#[instruction(agent_id: String)]
pub struct RegisterAgent<'info> {
/// The agent registry entry account (PDA) to be created
#[account(
init,
payer = payer,
space = AgentRegistryEntryV1::ACCOUNT_SIZE,
seeds = [AGENT_REGISTRY_PDA_SEED, agent_id.as_bytes()],
bump
)]
pub agent_entry: Account<'info, AgentRegistryEntryV1>,
/// The authority that will own and control this agent entry
#[account(mut)]
pub owner_authority: Signer<'info>,
/// The account that will pay for the rent of the new agent entry account
#[account(mut)]
pub payer: Signer<'info>,
/// The system program to create the new account
pub system_program: Program<'info, System>,
}
/// Accounts required for agent update instructions (update_agent_details, update_agent_status, deregister_agent)
///
/// This struct defines the accounts that must be provided when calling agent update instructions.
/// It verifies that the signer matches the owner_authority of the agent entry.
#[derive(Accounts)]
pub struct UpdateAgent<'info> {
/// The existing agent registry entry account to be modified
#[account(
mut,
seeds = [AGENT_REGISTRY_PDA_SEED, agent_entry.agent_id.as_bytes()],
bump = agent_entry.bump,
has_one = owner_authority @ ErrorCode::Unauthorized
)]
pub agent_entry: Account<'info, AgentRegistryEntryV1>,
/// The authority that owns this agent entry, must sign to authorize changes
#[account(mut)]
pub owner_authority: Signer<'info>,
}
/// Accounts required for the register_mcp_server instruction
///
/// This struct defines the accounts that must be provided when calling the register_mcp_server instruction.
/// It creates a new PDA for the MCP server entry, using the server_id as a seed.
#[derive(Accounts)]
#[instruction(server_id: String)]
pub struct RegisterMcpServer<'info> {
/// The MCP server registry entry account (PDA) to be created
#[account(
init,
payer = payer,
space = McpServerRegistryEntryV1::ACCOUNT_SIZE,
seeds = [MCP_SERVER_REGISTRY_PDA_SEED, server_id.as_bytes()],
bump
)]
pub mcp_server_entry: Account<'info, McpServerRegistryEntryV1>,
/// The authority that will own and control this MCP server entry
#[account(mut)]
pub owner_authority: Signer<'info>,
/// The account that will pay for the rent of the new MCP server entry account
#[account(mut)]
pub payer: Signer<'info>,
/// The system program to create the new account
pub system_program: Program<'info, System>,
}
/// Accounts required for MCP server update instructions (update_mcp_server_details, update_mcp_server_status, deregister_mcp_server)
///
/// This struct defines the accounts that must be provided when calling MCP server update instructions.
/// It verifies that the signer matches the owner_authority of the MCP server entry.
#[derive(Accounts)]
pub struct UpdateMcpServer<'info> {
/// The existing MCP server registry entry account to be modified
#[account(
mut,
seeds = [MCP_SERVER_REGISTRY_PDA_SEED, mcp_server_entry.server_id.as_bytes()],
bump = mcp_server_entry.bump,
has_one = owner_authority @ ErrorCode::Unauthorized
)]
pub mcp_server_entry: Account<'info, McpServerRegistryEntryV1>,
/// The authority that owns this MCP server entry, must sign to authorize changes
#[account(mut)]
pub owner_authority: Signer<'info>,
}
// --- State Structs (Accounts) ---
/// Agent Registry Entry (V1) - Solana account structure for storing agent data on-chain
///
/// This structure represents the on-chain data for a registered agent, following a hybrid approach:
/// - Core, frequently accessed data is stored directly on-chain
/// - Large or complex data can be referenced via off-chain URIs
/// - Verification hashes ensure integrity of off-chain data
///
/// The struct maps closely to concepts from the A2A AgentCard specification and AEA framework,
/// optimized for Solana's account model and storage constraints.
#[account]
#[derive(Default, Clone)]
pub struct AgentRegistryEntryV1 {
/// Bump seed used for this PDA's derivation
pub bump: u8, // 1
/// Schema version of this entry (e.g., 1)
pub registry_version: u8, // 1
/// Solana public key of the entry's owner/manager
pub owner_authority: Pubkey, // 32
/// Unique identifier for the agent
pub agent_id: String, // 4 + MAX_AGENT_ID_LEN
/// Human-readable name of the agent
pub name: String, // 4 + MAX_AGENT_NAME_LEN
/// Human-readable description of the agent (can use CommonMark)
pub description: String, // 4 + MAX_AGENT_DESCRIPTION_LEN
/// Version of the agent software/implementation
pub agent_version: String, // 4 + MAX_AGENT_VERSION_LEN
/// Optional name of the agent's provider organization
pub provider_name: Option<String>, // 1 + (4 + MAX_PROVIDER_NAME_LEN)
/// Optional URL of the agent's provider
pub provider_url: Option<String>, // 1 + (4 + MAX_PROVIDER_URL_LEN)
/// Optional URL to human-readable documentation
pub documentation_url: Option<String>, // 1 + (4 + MAX_DOCUMENTATION_URL_LEN)
/// List of service endpoints where the agent can be reached
pub service_endpoints: Vec<ServiceEndpoint>, // 4 + MAX_SERVICE_ENDPOINTS * ServiceEndpoint::ACCOUNT_SIZE
/// Bitmask for core A2A capabilities
pub capabilities_flags: u64, // 8
/// Default accepted input MIME types
pub supported_input_modes: Vec<String>, // 4 + MAX_SUPPORTED_MODES * (4 + MAX_MODE_LEN)
/// Default produced output MIME types
pub supported_output_modes: Vec<String>,// 4 + MAX_SUPPORTED_MODES * (4 + MAX_MODE_LEN)
/// Summary of key agent skills
pub skills: Vec<AgentSkill>, // 4 + MAX_SKILLS * AgentSkill::ACCOUNT_SIZE
/// Optional URI to detailed security scheme definitions (e.g., OpenAPI format)
pub security_info_uri: Option<String>, // 1 + (4 + MAX_SECURITY_INFO_URI_LEN)
/// Optional Fetch.ai AEA address/ID
pub aea_address: Option<String>, // 1 + (4 + MAX_AEA_ADDRESS_LEN)
/// Optional brief summary of the agent's economic goals
pub economic_intent_summary: Option<String>, // 1 + (4 + MAX_ECONOMIC_INTENT_LEN)
/// Optional SHA256 hash of a list of supported AEA protocol IDs
pub supported_aea_protocols_hash: Option<[u8; HASH_SIZE]>, // 1 + HASH_SIZE
/// Agent status (0:Pending, 1:Active, 2:Inactive, 3:Deregistered)
pub status: u8, // 1 (Enum AgentStatus)
/// Timestamp of initial registration
pub registration_timestamp: i64, // 8
/// Timestamp of the last update
pub last_update_timestamp: i64, // 8
/// Optional URI to extensive off-chain metadata (e.g., full AgentCard JSON)
pub extended_metadata_uri: Option<String>,// 1 + (4 + MAX_EXTENDED_METADATA_URI_LEN)
/// General discoverability tags for the agent
pub tags: Vec<String>, // 4 + MAX_AGENT_TAGS * (4 + MAX_AGENT_TAG_LEN)
}
/// Implementation of the AccountSize trait for AgentRegistryEntryV1
///
/// Calculates the total space required for an AgentRegistryEntryV1 account when serialized with Borsh.
/// This includes the Anchor discriminator and all fields with their maximum possible sizes.
impl AccountSize for AgentRegistryEntryV1 { // Renamed from LEN to ACCOUNT_SIZE for consistency with trait
const ACCOUNT_SIZE: usize = 8 // Anchor Discriminator
+ 1 // bump
+ 1 // registry_version
+ 32 // owner_authority
+ borsh_size_string(MAX_AGENT_ID_LEN)
+ borsh_size_string(MAX_AGENT_NAME_LEN)
+ borsh_size_string(MAX_AGENT_DESCRIPTION_LEN)
+ borsh_size_string(MAX_AGENT_VERSION_LEN)
+ borsh_size_option_string(MAX_PROVIDER_NAME_LEN)
+ borsh_size_option_string(MAX_PROVIDER_URL_LEN)
+ borsh_size_option_string(MAX_DOCUMENTATION_URL_LEN)
+ STRING_LEN_PREFIX_SIZE + (MAX_SERVICE_ENDPOINTS * ServiceEndpoint::ACCOUNT_SIZE) // service_endpoints
+ 8 // capabilities_flags
+ borsh_size_vec_string(MAX_SUPPORTED_MODES, MAX_MODE_LEN) // supported_input_modes
+ borsh_size_vec_string(MAX_SUPPORTED_MODES, MAX_MODE_LEN) // supported_output_modes
+ STRING_LEN_PREFIX_SIZE + (MAX_SKILLS * AgentSkill::ACCOUNT_SIZE) // skills
+ borsh_size_option_string(MAX_SECURITY_INFO_URI_LEN)
+ borsh_size_option_string(MAX_AEA_ADDRESS_LEN)
+ borsh_size_option_string(MAX_ECONOMIC_INTENT_LEN)
+ borsh_size_option_hash() // supported_aea_protocols_hash
+ 1 // status
+ 8 // registration_timestamp
+ 8 // last_update_timestamp
+ borsh_size_option_string(MAX_EXTENDED_METADATA_URI_LEN)
+ borsh_size_vec_string(MAX_AGENT_TAGS, MAX_AGENT_TAG_LEN); // tags
}
/// Service Endpoint definition for agents
///
/// Represents an endpoint where an agent can be accessed, including the protocol
/// type (e.g., "a2a_http_jsonrpc") and URL, with a flag indicating if this is
/// the default endpoint for the agent.
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Default, Debug)]
pub struct ServiceEndpoint {
/// Protocol type (e.g., "a2a_http_jsonrpc", "aea_p2p")
pub protocol: String,
/// Endpoint URL
pub url: String,
/// Indicates if this is the primary endpoint (only one can be true)
pub is_default: bool,
}
/// Implementation of the AccountSize trait for ServiceEndpoint
impl AccountSize for ServiceEndpoint {
const ACCOUNT_SIZE: usize = borsh_size_string(MAX_ENDPOINT_PROTOCOL_LEN)
+ borsh_size_string(MAX_ENDPOINT_URL_LEN)
+ 1; // is_default (bool)
}
/// Agent Skill definition
///