MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / createRelations

Method createRelations

examples/server/memory/kb.go:209–233  ·  view source on GitHub ↗

createRelations adds new relations to the graph, skipping exact duplicates. It returns the new relations that were actually added.

(relations []Relation)

Source from the content-addressed store, hash-verified

207// createRelations adds new relations to the graph, skipping exact duplicates.
208// It returns the new relations that were actually added.
209func (k knowledgeBase) createRelations(relations []Relation) ([]Relation, error) {
210 graph, err := k.loadGraph()
211 if err != nil {
212 return nil, err
213 }
214
215 var newRelations []Relation
216 for _, relation := range relations {
217 exists := slices.ContainsFunc(graph.Relations, func(r Relation) bool {
218 return r.From == relation.From &&
219 r.To == relation.To &&
220 r.RelationType == relation.RelationType
221 })
222 if !exists {
223 newRelations = append(newRelations, relation)
224 graph.Relations = append(graph.Relations, relation)
225 }
226 }
227
228 if err := k.saveGraph(graph); err != nil {
229 return nil, err
230 }
231
232 return newRelations, nil
233}
234
235// addObservations appends new observations to existing entities.
236// It returns the new observations that were actually added.

Callers 3

CreateRelationsMethod · 0.95

Calls 2

loadGraphMethod · 0.95
saveGraphMethod · 0.95

Tested by 2