Files
mindoc/mcp/middleware.go
nl8590687 df09353563
Some checks failed
Go / Ubuntu Latest GCC (push) Has been cancelled
Go / Windows Latest MSVC (push) Has been cancelled
feat: 新增MCP Server功能,支持文档全局检索工具 (#1010)
* feat: i18n国际化语言支持完全可配置,并新增俄语支持

* feat: 新增MCP Server功能,支持文档全局检索工具
2025-09-22 17:57:05 +08:00

23 lines
721 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package mcp
import (
"context"
"net/http"
"github.com/beego/beego/v2/server/web"
beegoContext "github.com/beego/beego/v2/server/web/context"
)
// AuthMiddleware 返回一个中间件函数用于验证MCP请求中的认证令牌
func AuthMiddleware(ctx *beegoContext.Context) {
presetMcpApiKey := web.AppConfig.DefaultString("mcp_api_key", "")
mcpApiKeyParamValue := ctx.Request.URL.Query().Get("api_key")
if presetMcpApiKey != mcpApiKeyParamValue {
http.Error(ctx.ResponseWriter, "Missing or invalid mcp authorization key", http.StatusUnauthorized)
return
}
// Add mcp_api_key to request context
ctx.Request.WithContext(context.WithValue(ctx.Request.Context(), "mcp_api_key", mcpApiKeyParamValue))
}