add mcp (Model Context Protocol) support
This commit is contained in:
@@ -2,6 +2,7 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
@@ -212,6 +213,27 @@ func startWebServer(c *core.CliContext) error {
|
||||
qrCodeRoute.GET("/mobile_url.png", bindCachedImage(api.QrCodes.MobileUrlQrCodeHandler, qrCodeCacheStore))
|
||||
}
|
||||
|
||||
if config.EnableMCPServer {
|
||||
mcpRoute := router.Group("/mcp")
|
||||
mcpRoute.Use(bindMiddleware(middlewares.RequestId(config)))
|
||||
mcpRoute.Use(bindMiddleware(middlewares.RequestLog))
|
||||
mcpRoute.Use(bindMiddleware(middlewares.MCPServerIpLimit(config)))
|
||||
mcpRoute.Use(bindMiddleware(middlewares.JWTAuthorization))
|
||||
{
|
||||
mcpRoute.POST("", bindJSONRPCApi(map[string]core.JSONRPCApiHandlerFunc{
|
||||
"initialize": api.ModelContextProtocols.InitializeHandler,
|
||||
"resources/list": api.ModelContextProtocols.ListResourcesHandler,
|
||||
"resources/read": api.ModelContextProtocols.ReadResourceHandler,
|
||||
"tools/list": api.ModelContextProtocols.ListToolsHandler,
|
||||
"tools/call": api.ModelContextProtocols.CallToolHandler,
|
||||
"ping": api.ModelContextProtocols.PingHandler,
|
||||
}, map[string]int{
|
||||
"notifications/initialized": http.StatusAccepted,
|
||||
}))
|
||||
mcpRoute.GET("", bindApi(api.Default.MethodNotAllowed))
|
||||
}
|
||||
}
|
||||
|
||||
apiRoute := router.Group("/api")
|
||||
|
||||
apiRoute.Use(bindMiddleware(middlewares.RequestId(config)))
|
||||
@@ -432,6 +454,44 @@ func bindApiWithTokenUpdate(fn core.ApiHandlerFunc, config *settings.Config) gin
|
||||
}
|
||||
}
|
||||
|
||||
func bindJSONRPCApi(fns map[string]core.JSONRPCApiHandlerFunc, skipMethods map[string]int) gin.HandlerFunc {
|
||||
return func(ginCtx *gin.Context) {
|
||||
c := core.WrapWebContext(ginCtx)
|
||||
|
||||
var jsonRPCRequest core.JSONRPCRequest
|
||||
reqErr := c.ShouldBindBodyWithJSON(&jsonRPCRequest)
|
||||
|
||||
if reqErr != nil {
|
||||
utils.PrintJSONRPCErrorResult(c, nil, errs.NewIncompleteOrIncorrectSubmissionError(reqErr))
|
||||
return
|
||||
}
|
||||
|
||||
if skipMethods != nil {
|
||||
httpStatusCode, exists := skipMethods[jsonRPCRequest.Method]
|
||||
|
||||
if exists {
|
||||
c.AbortWithStatus(httpStatusCode)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
fn, exists := fns[jsonRPCRequest.Method]
|
||||
|
||||
if !exists {
|
||||
utils.PrintJSONRPCErrorResult(c, &jsonRPCRequest, errs.ErrApiNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := fn(c, &jsonRPCRequest)
|
||||
|
||||
if err != nil {
|
||||
utils.PrintJSONRPCErrorResult(c, &jsonRPCRequest, err)
|
||||
} else {
|
||||
utils.PrintJSONRPCSuccessResult(c, &jsonRPCRequest, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func bindEventStreamApi(fn core.EventStreamApiHandlerFunc) gin.HandlerFunc {
|
||||
return func(ginCtx *gin.Context) {
|
||||
c := core.WrapWebContext(ginCtx)
|
||||
|
||||
Reference in New Issue
Block a user