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

Abierto Apto para principiantes
#1,557 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
82/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
go

Línea de trabajo

Lee toolForErr en mcp/server.go e inspecciona el comportamiento de la caché del esquema con entradas any. Añade una prueba de regresión que llame repetidamente a AddTool con un SchemaCache compartido y confirme que la caché no crece con cada llamada. La tarea estará completa cuando la prueba pase y el uso de memoria se mantenga acotado, como ocurre con las entradas struct.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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

Lenguaje dominante
Go
Estrellas
5.4k
Forks
623
Merge medio
22 h 10 min
PR fusionados (30 d)
30

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de modelcontextprotocol/go-sdk

Todos los issues de modelcontextprotocol/go-sdk

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.