flipped-aurora/gin-vue-admin · error

参数错误:userRequirement 必须是字符串类型

Error message

参数错误:userRequirement 必须是字符串类型

What it means

After confirming 'userRequirement' exists, Handle performs a Go type assertion userRequirementData.(string). The value must be a JSON string; if the argument was supplied as a number, object, array, bool or nil, the assertion fails and this error is returned. MCP arguments are loosely typed on the wire, so the server must defensively assert the expected type.

Source

Thrown at server/mcp/gva_review.go:70

		),
		mcp.WithString("generatedFiles",
			mcp.Description("gva_execute创建的文件列表,JSON字符串格式,包含所有生成的后端和前端文件路径"),
			mcp.Required(),
		),
	)
}

// Handle 处理审查请求
func (g *GVAReviewer) Handle(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
	// 获取用户需求
	userRequirementData, ok := request.GetArguments()["userRequirement"]
	if !ok {
		return nil, errors.New("参数错误:userRequirement 必须提供")
	}

	userRequirement, ok := userRequirementData.(string)
	if !ok {
		return nil, errors.New("参数错误:userRequirement 必须是字符串类型")
	}

	// 获取生成的文件列表
	generatedFilesData, ok := request.GetArguments()["generatedFiles"]
	if !ok {
		return nil, errors.New("参数错误:generatedFiles 必须提供")
	}

	generatedFilesStr, ok := generatedFilesData.(string)
	if !ok {
		return nil, errors.New("参数错误:generatedFiles 必须是JSON字符串")
	}

	// 解析JSON字符串为字符串数组
	var generatedFiles []string
	err := json.Unmarshal([]byte(generatedFilesStr), &generatedFiles)
	if err != nil {
		return nil, fmt.Errorf("解析generatedFiles失败: %v", err)

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Send 'userRequirement' as a JSON string: {"userRequirement": "your requirement text"}.
  2. Coerce the value to string on the client before invoking the tool (String(value) or equivalent).
  3. Inspect the actual payload sent over the wire with a logger to confirm the runtime JSON type.
  4. If the requirement contains structured data, serialize it to a string yourself before passing it.

Example fix

// before
{"userRequirement": 42}
// after
{"userRequirement": "42 - 实现对应功能"}
Defensive patterns

Strategy: type-guard

Validate before calling

req := map[string]any{"userRequirement": fmt.Sprintf("%v", requirementText)}
if _, ok := req["userRequirement"].(string); !ok {
    return errors.New("userRequirement must serialize as a JSON string")
}

Type guard

func isString(v any) bool {
    _, ok := v.(string)
    return ok
}

Prevention

When it happens

Trigger: Calling gva_review with 'userRequirement' present but not a JSON string, e.g. {"userRequirement": 123} or {"userRequirement": {"text": "..."}}.

Common situations: Client serialization turning the string into a number/bool; caller passing a nested object because they misunderstood the tool schema; JSON generators quoting/escaping incorrectly in the other direction (typed values).

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/1dcd5a1f21501535. Report an issue: GitHub.