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

Đang mở Phù hợp với người mới
#1,557 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
82/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
go
Lĩnh vực
backend-api-design

Hướng nghiên cứu

Đọc toolForErr trong mcp/server.go và kiểm tra hành vi của bộ nhớ đệm schema với đầu vào any. Thêm một bài kiểm thử hồi quy gọi AddTool nhiều lần với một SchemaCache dùng chung và xác nhận bộ nhớ đệm không tăng lên sau mỗi lần gọi. Công việc được xem là hoàn tất khi bài kiểm thử chạy qua và mức sử dụng bộ nhớ được giới hạn, như với đầu vào struct.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
Go
Star
5.4k
Fork
623
Merge trung bình
22 giờ 10 phút
Pull request đã merge (30 ngày)
30

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của modelcontextprotocol/go-sdk

Tất cả issue của modelcontextprotocol/go-sdk

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.