| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package controller
- import (
- "net/http"
- "strings"
- "github.com/gin-gonic/gin"
- "github.com/labring/aiproxy/core/common"
- "github.com/labring/aiproxy/core/common/config"
- "github.com/labring/aiproxy/core/middleware"
- "github.com/labring/aiproxy/core/model"
- mcpservers "github.com/labring/aiproxy/mcp-servers"
- "github.com/mark3labs/mcp-go/mcp"
- )
- func routeHostMCP(
- c *gin.Context,
- publicHandler, groupHandler func(c *gin.Context, mcpID string),
- ) {
- log := common.GetLogger(c)
- host := c.Request.Host
- log.Debugf("route host mcp: %s", host)
- publicMCPHost := config.GetPublicMCPHost()
- groupMCPHost := config.GetGroupMCPHost()
- switch {
- case publicMCPHost != "" && strings.HasSuffix(host, publicMCPHost):
- mcpID := strings.TrimSuffix(host, "."+publicMCPHost)
- publicHandler(c, mcpID)
- case groupMCPHost != "" && strings.HasSuffix(host, groupMCPHost):
- mcpID := strings.TrimSuffix(host, "."+groupMCPHost)
- groupHandler(c, mcpID)
- default:
- http.Error(c.Writer, "invalid host", http.StatusNotFound)
- }
- }
- // HostMCPSSEServer godoc
- //
- // @Summary Public MCP SSE Server
- // @Security ApiKeyAuth
- // @Router /sse [get]
- func HostMCPSSEServer(c *gin.Context) {
- routeHostMCP(c, func(c *gin.Context, mcpID string) {
- publicMcp, err := model.CacheGetPublicMCP(mcpID)
- if err != nil {
- http.Error(c.Writer, err.Error(), http.StatusBadRequest)
- return
- }
- if publicMcp.Status != model.PublicMCPStatusEnabled {
- http.Error(c.Writer, "mcp is not enabled", http.StatusBadRequest)
- return
- }
- group := middleware.GetGroup(c)
- paramsFunc := newGroupParams(publicMcp.ID, group.ID)
- handlePublicSSEMCP(c, publicMcp, paramsFunc, sseEndpoint)
- }, func(c *gin.Context, mcpID string) {
- group := middleware.GetGroup(c)
- groupMcp, err := model.CacheGetGroupMCP(group.ID, mcpID)
- if err != nil {
- http.Error(c.Writer, err.Error(), http.StatusNotFound)
- return
- }
- if groupMcp.Status != model.GroupMCPStatusEnabled {
- http.Error(c.Writer, "mcp is not enabled", http.StatusNotFound)
- return
- }
- handleGroupSSEMCPServer(c, groupMcp, sseEndpoint)
- })
- }
- // HostMCPStreamable godoc
- //
- // @Summary Host MCP Streamable Server
- // @Security ApiKeyAuth
- // @Router /mcp [get]
- // @Router /mcp [post]
- // @Router /mcp [delete]
- func HostMCPStreamable(c *gin.Context) {
- routeHostMCP(c, func(c *gin.Context, mcpID string) {
- publicMcp, err := model.CacheGetPublicMCP(mcpID)
- if err != nil {
- c.JSON(http.StatusBadRequest, mcpservers.CreateMCPErrorResponse(
- mcp.NewRequestId(nil),
- mcp.INVALID_REQUEST,
- err.Error(),
- ))
- return
- }
- if publicMcp.Status != model.PublicMCPStatusEnabled {
- c.JSON(http.StatusNotFound, mcpservers.CreateMCPErrorResponse(
- mcp.NewRequestId(nil),
- mcp.INVALID_REQUEST,
- "mcp is not enabled",
- ))
- return
- }
- group := middleware.GetGroup(c)
- paramsFunc := newGroupParams(publicMcp.ID, group.ID)
- handlePublicStreamable(c, publicMcp, paramsFunc)
- }, func(c *gin.Context, mcpID string) {
- group := middleware.GetGroup(c)
- groupMcp, err := model.CacheGetGroupMCP(group.ID, mcpID)
- if err != nil {
- c.JSON(http.StatusNotFound, mcpservers.CreateMCPErrorResponse(
- mcp.NewRequestId(nil),
- mcp.INVALID_REQUEST,
- err.Error(),
- ))
- return
- }
- if groupMcp.Status != model.GroupMCPStatusEnabled {
- c.JSON(http.StatusNotFound, mcpservers.CreateMCPErrorResponse(
- mcp.NewRequestId(nil),
- mcp.INVALID_REQUEST,
- "mcp is not enabled",
- ))
- return
- }
- handleGroupStreamable(c, groupMcp)
- })
- }
|