extractToolsFromFrontmatter extracts tools and mcp-servers sections from an already-parsed frontmatter map as a JSON string. This avoids re-parsing YAML when the frontmatter has already been parsed, which is the common case for imported files (both builtin and local).
(frontmatter map[string]any)
| 58 | // frontmatter map as a JSON string. This avoids re-parsing YAML when the frontmatter has |
| 59 | // already been parsed, which is the common case for imported files (both builtin and local). |
| 60 | func extractToolsFromFrontmatter(frontmatter map[string]any) (string, error) { |
| 61 | // Create a map to hold the merged result |
| 62 | extracted := make(map[string]any) |
| 63 | |
| 64 | // Helper function to merge a field into extracted map. |
| 65 | // Non-map values (e.g. arrays in custom-agent format) are skipped here; |
| 66 | // schema validation elsewhere will report them as invalid if appropriate. |
| 67 | mergeField := func(fieldName string) { |
| 68 | if fieldValue, exists := frontmatter[fieldName]; exists { |
| 69 | if fieldMap, ok := fieldValue.(map[string]any); ok { |
| 70 | maps.Copy(extracted, fieldMap) |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Extract and merge tools section (tools are stored as tool_name: tool_config) |
| 76 | mergeField("tools") |
| 77 | |
| 78 | // Extract and merge mcp-servers section (mcp-servers are stored as server_name: server_config) |
| 79 | mergeField("mcp-servers") |
| 80 | |
| 81 | // If nothing was extracted, return empty object |
| 82 | if len(extracted) == 0 { |
| 83 | return "{}", nil |
| 84 | } |
| 85 | |
| 86 | // Convert to JSON string. On marshal failure, return an empty object to |
| 87 | // match the behaviour of extractToolsFromContent (defensive, not a hard error). |
| 88 | extractedJSON, err := json.Marshal(extracted) |
| 89 | if err != nil { |
| 90 | return "{}", nil |
| 91 | } |
| 92 | |
| 93 | return strings.TrimSpace(string(extractedJSON)), nil |
| 94 | } |
| 95 | |
| 96 | // extractFrontmatterField extracts a specific field from frontmatter as JSON string |
| 97 | func extractFrontmatterField(content, fieldName, emptyValue string) (string, error) { |
no outgoing calls
no test coverage detected