MCPcopy
hub / github.com/googleapis/mcp-toolbox / UnmarshalResourceConfig

Function UnmarshalResourceConfig

internal/server/config.go:166–299  ·  view source on GitHub ↗
(ctx context.Context, raw []byte)

Source from the content-addressed store, hash-verified

164type PromptsetConfigs map[string]prompts.PromptsetConfig
165
166func UnmarshalResourceConfig(ctx context.Context, raw []byte) (SourceConfigs, AuthServiceConfigs, EmbeddingModelConfigs, ToolConfigs, ToolsetConfigs, PromptConfigs, error) {
167 // prepare configs map
168 var sourceConfigs SourceConfigs
169 var authServiceConfigs AuthServiceConfigs
170 var embeddingModelConfigs EmbeddingModelConfigs
171 var toolConfigs ToolConfigs
172 var toolsetConfigs ToolsetConfigs
173 var promptConfigs PromptConfigs
174 // promptset configs is not yet supported
175
176 file, err := parser.ParseBytes(raw, 0)
177 if err != nil {
178 return nil, nil, nil, nil, nil, nil, fmt.Errorf("unable to parse YAML: %s", yaml.FormatError(err, false, false))
179 }
180
181 decoder := yaml.NewDecoder(bytes.NewReader(raw))
182 for index, doc := range file.Docs {
183 if doc == nil || doc.Body == nil {
184 continue
185 }
186 docIndex := index + 1
187 var resource map[string]any
188 if err := decoder.DecodeFromNodeContext(ctx, doc.Body, &resource); err != nil {
189 if len(file.Docs) > 1 {
190 return nil, nil, nil, nil, nil, nil, fmt.Errorf("document %d: %s", docIndex, yaml.FormatError(err, false, false))
191 }
192 return nil, nil, nil, nil, nil, nil, fmt.Errorf("unable to decode YAML document: %s", yaml.FormatError(err, false, false))
193 }
194 var kind, name string
195 var ok bool
196 if kind, ok = resource["kind"].(string); !ok {
197 if len(file.Docs) > 1 {
198 return nil, nil, nil, nil, nil, nil, fmt.Errorf("%s missing 'kind' field or it is not a string", formatDocLocation(docIndex, keyToken(doc.Body, "kind"), doc.Body))
199 }
200 return nil, nil, nil, nil, nil, nil, fmt.Errorf("missing 'kind' field or it is not a string: %v", resource)
201 }
202 if name, ok = resource["name"].(string); !ok {
203 if len(file.Docs) > 1 {
204 fallbackToken := keyToken(doc.Body, "name")
205 if fallbackToken == nil {
206 fallbackToken = keyToken(doc.Body, "kind")
207 }
208 return nil, nil, nil, nil, nil, nil, fmt.Errorf("%s missing 'name' field or it is not a string", formatDocLocation(docIndex, fallbackToken, doc.Body))
209 }
210 return nil, nil, nil, nil, nil, nil, fmt.Errorf("missing 'name' field or it is not a string")
211 }
212 // remove 'kind' from map for strict unmarshaling
213 delete(resource, "kind")
214
215 switch kind {
216 case "source":
217 c, err := UnmarshalYAMLSourceConfig(ctx, name, resource)
218 if err != nil {
219 if len(file.Docs) > 1 {
220 return nil, nil, nil, nil, nil, nil, fmt.Errorf("document %d: error unmarshaling %s %q: %w", docIndex, kind, name, err)
221 }
222 return nil, nil, nil, nil, nil, nil, fmt.Errorf("error unmarshaling %s: %w", kind, err)
223 }

Calls 8

formatDocLocationFunction · 0.85
keyTokenFunction · 0.85
UnmarshalYAMLToolConfigFunction · 0.85