mcp: SchemaCache grows by one entry per AddTool call for tools with an any input

Aperta Adatta ai principianti
#1,557 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go

Direzione di ricerca

Leggi toolForErr in mcp/server.go ed esamina il comportamento della cache dello schema con input any. Aggiungi un test di regressione che chiami ripetutamente AddTool con un SchemaCache condiviso e confermi che la cache non cresce a ogni chiamata. Il lavoro è completato quando il test passa e l’uso della memoria rimane limitato, come per gli input struct.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Checklist
  • I searched the existing issues, and this has not been reported before.
  • This is a real problem that I ran into myself, using the latest release of the SDK.
  • I ran into it in a production system, not in a toy example, a benchmark, or an automated or AI-driven scan of the code.
  • I (a human) examined the relevant SDK code, and wrote or reviewed every line of this report. It is not unreviewed AI output.
  • This report is brief and concise, and sticks to the facts needed to understand the problem.
What happened?

With a shared SchemaCache, every AddTool call for a tool whose input type is any adds a new cache entry that is never used again. So a stateless server (new Server per request, one shared cache) keeps using more memory forever. Tools with a struct input don't have this problem.

Cause: toolForErr (mcp/server.go) makes a new &jsonschema.Schema{Type: "object"} for an any input on every call. The cache stores schemas by pointer, so it never finds the old one and adds a new entry each time.

Expected: memory stays flat, like it does for struct inputs.

I'd like to fix this (small change in toolForErr plus a test) if that's OK. I used an AI assistant to help find this and draft the report; I ran the repro and checked the code myself.

How to reproduce
package main

import (
	"context"
	"fmt"
	"runtime"

	"github.com/modelcontextprotocol/go-sdk/mcp"
)

func main() {
	cache := mcp.NewSchemaCache()
	for i := 0; i < 100_000; i++ {
		s := mcp.NewServer(&mcp.Implementation{Name: "s", Version: "v0"}, &mcp.ServerOptions{SchemaCache: cache})
		mcp.AddTool(s, &mcp.Tool{Name: "ping"}, func(context.Context, *mcp.CallToolRequest, any) (*mcp.CallToolResult, any, error) {
			return nil, nil, nil
		})
	}
	runtime.GC()
	var m runtime.MemStats
	runtime.ReadMemStats(&m)
	fmt.Printf("heap: %.0f MB\n", float64(m.HeapAlloc)/(1<<20))
	runtime.KeepAlive(cache)
}

Prints about heap: 168 MB. With a struct input instead of any, it stays under 1 MB.

Go MCP SDK version

v1.8.0 (also on main at 13d3c71)

Go version

go1.27.4 windows/amd64

Lingua principale
Go
Stelle
5.4k
Fork
623
Merge medio
22h 10m
PR unite (30g)
30

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di modelcontextprotocol/go-sdk

Tutte le issue di modelcontextprotocol/go-sdk

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.