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

オープン 初心者向け
#1,557 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
82/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
go

調査の方向性

mcp/server.go の toolForErr を読み、any 入力に対するスキーマキャッシュの動作を調べてください。共有の SchemaCache を使って AddTool を繰り返し呼び出し、呼び出すたびにキャッシュが増加しないことを確認する回帰テストを追加してください。テストが成功し、struct 入力の場合と同様にメモリ使用量が一定の範囲内に収まれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
Go
スター
5.4k
フォーク
623
平均マージ
22時間 10分
マージ済み PR(30日)
30

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

modelcontextprotocol/go-sdk のほかの issue

modelcontextprotocol/go-sdk の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。