@@ -21,13 +21,14 @@ import (
2121var _ IMcpController = (* imlMcpController )(nil )
2222
2323type imlMcpController struct {
24- settingModule system.ISettingModule `autowired:""`
25- authorizationModule application_authorization.IAuthorizationModule `autowired:""`
26- appModule service.IAppModule `autowired:""`
27- mcpModule mcp.IMcpModule `autowired:""`
28- sessionKeys sync.Map
29- server map [string ]http.Handler
30- openServer http.Handler
24+ settingModule system.ISettingModule `autowired:""`
25+ authorizationModule application_authorization.IAuthorizationModule `autowired:""`
26+ appModule service.IAppModule `autowired:""`
27+ mcpModule mcp.IMcpModule `autowired:""`
28+ sessionKeys sync.Map
29+ sseServers map [string ]http.Handler
30+ openSseServer http.Handler
31+ openStreamableServer http.Handler
3132}
3233
3334func (i * imlMcpController ) AppMCPHandle (ctx * gin.Context ) {
@@ -42,12 +43,12 @@ func (i *imlMcpController) AppMCPHandle(ctx *gin.Context) {
4243 paths := strings .Split (req .URL .Path , "/" )
4344 req .URL .Path = fmt .Sprintf ("/api/v1/%s/%s" , mcp_server .GlobalBasePath , paths [len (paths )- 1 ])
4445 locale := utils .I18n (ctx )
45- if v , ok := i .server [locale ]; ok {
46+ if v , ok := i .sseServers [locale ]; ok {
4647 v .ServeHTTP (ctx .Writer , req )
4748 return
4849 }
4950
50- i .server [languageEnUs ].ServeHTTP (ctx .Writer , req )
51+ i .sseServers [languageEnUs ].ServeHTTP (ctx .Writer , req )
5152}
5253
5354func (i * imlMcpController ) AppHandleSSE (ctx * gin.Context ) {
@@ -68,7 +69,7 @@ func (i *imlMcpController) AppHandleSSE(ctx *gin.Context) {
6869 }
6970
7071 ctx .Request .URL .Path = fmt .Sprintf ("/openapi/v1/%s/sse" , mcp_server .GlobalBasePath )
71- i .handleSSE (ctx , i .openServer , SessionInfo {
72+ i .handleSSE (ctx , i .openSseServer , SessionInfo {
7273 Apikey : apikey ,
7374 App : appId ,
7475 })
@@ -81,8 +82,29 @@ func (i *imlMcpController) AppHandleMessage(ctx *gin.Context) {
8182 return
8283 }
8384 ctx .Request .URL .Path = fmt .Sprintf ("/openapi/v1/%s/message" , mcp_server .GlobalBasePath )
84- ctx .Request = ctx .Request .WithContext (utils .SetLabel (ctx .Request .Context (), "app" , appId ))
85- i .handleMessage (ctx , i .openServer )
85+ //ctx.Request = ctx.Request.WithContext(utils.SetLabel(ctx.Request.Context(), "app", appId))
86+ i .handleMessage (ctx , i .openSseServer )
87+ }
88+
89+ func (i * imlMcpController ) AppHandleStreamHTTP (ctx * gin.Context ) {
90+ apikey := ctx .Request .Header .Get ("Authorization" )
91+ apikey = strings .TrimPrefix (apikey , "Bearer " )
92+ if apikey == "" {
93+ ctx .AbortWithStatusJSON (403 , gin.H {"code" : - 1 , "msg" : "invalid apikey" , "success" : "fail" })
94+ return
95+ }
96+ appId := ctx .Request .Header .Get ("X-Application-Id" )
97+ if appId == "" {
98+ ctx .AbortWithStatusJSON (403 , gin.H {"code" : - 1 , "msg" : "invalid app id" , "success" : "fail" })
99+ return
100+ }
101+ cfg := i .settingModule .Get (ctx )
102+ req := ctx .Request .WithContext (utils .SetGatewayInvoke (ctx .Request .Context (), cfg .InvokeAddress ))
103+
104+ req = req .WithContext (utils .SetLabel (req .Context (), "apikey" , apikey ))
105+ req = req .WithContext (utils .SetLabel (req .Context (), "app" , appId ))
106+ req .URL .Path = mcp_server .OpenGlobalMCPPath
107+ i .openStreamableServer .ServeHTTP (ctx .Writer , req )
86108}
87109
88110func (i * imlMcpController ) AppMCPConfig (ctx * gin.Context , appId string ) (string , error ) {
@@ -94,36 +116,44 @@ func (i *imlMcpController) AppMCPConfig(ctx *gin.Context, appId string) (string,
94116 if err != nil {
95117 return "" , fmt .Errorf ("get app info error: %v" , err )
96118 }
97- return fmt .Sprintf (mcpDefaultConfig , appInfo .Name , fmt .Sprintf ("%s/openapi/v1/mcp/app/%s/sse?apikey={your_api_key}" , strings .TrimSuffix (cfg .SitePrefix , "/" ), appId )), nil
98- }
99119
100- var mcpDefaultConfig = `{
101- "mcpServers": {
102- "%s": {
103- "url": "%s"
104- }
105- }
120+ return mcp_server .NewMCPConfig (
121+ mcp_server .TransportTypeStreamableHTTP ,
122+ fmt .Sprintf ("%s%s" , strings .TrimSuffix (cfg .SitePrefix , "/" ), mcp_server .OpenAppMCPPath ),
123+ map [string ]string {
124+ "Authorization" : "Bearer {your_api_key}" ,
125+ "X-Application-Id" : appId ,
126+ },
127+ nil ,
128+ ).ToString (appInfo .Name ), nil
106129}
107- `
108130
109131func (i * imlMcpController ) GlobalMCPConfig (ctx * gin.Context ) (string , error ) {
110132 cfg := i .settingModule .Get (ctx )
111133 if cfg .SitePrefix == "" {
112134 return "" , fmt .Errorf ("site prefix is empty" )
113135 }
114- return fmt .Sprintf (mcpDefaultConfig , "APIPark-MCP-Server" , fmt .Sprintf ("%s/openapi/v1/%s/sse?apikey={your_api_key}" , strings .TrimSuffix (cfg .SitePrefix , "/" ), mcp_server .GlobalBasePath )), nil
136+ return mcp_server .NewMCPConfig (
137+ mcp_server .TransportTypeStreamableHTTP ,
138+ fmt .Sprintf ("%s%s" , strings .TrimSuffix (cfg .SitePrefix , "/" ), mcp_server .OpenGlobalMCPPath ),
139+ map [string ]string {
140+ "Authorization" : "Bearer {your_api_key}" ,
141+ },
142+ nil ,
143+ ).ToString ("APIPark-MCP-Server" ), nil
115144}
116145
117146func (i * imlMcpController ) OnComplete () {
118- i .server = make (map [string ]http.Handler )
147+ i .sseServers = make (map [string ]http.Handler )
119148 for language , tools := range mcpToolsByLanguage {
120149 s := server .NewMCPServer ("APIPark MCP Server" , "1.0.0" , server .WithLogging ())
121150 s .AddTool (tools [ToolServiceList ], i .mcpModule .Services )
122151 s .AddTool (tools [ToolOpenAPIDocument ], i .mcpModule .APIs )
123152 s .AddTool (tools [ToolInvokeAPI ], i .mcpModule .Invoke )
124- i .server [language ] = server .NewSSEServer (s , server .WithStaticBasePath (fmt .Sprintf ("/api/v1/%s" , mcp_server .GlobalBasePath )))
153+ i .sseServers [language ] = server .NewSSEServer (s , server .WithStaticBasePath (fmt .Sprintf ("/api/v1/%s" , mcp_server .GlobalBasePath )))
125154 if language == languageEnUs {
126- i .openServer = server .NewSSEServer (s , server .WithStaticBasePath (fmt .Sprintf ("/openapi/v1/%s" , strings .Trim (mcp_server .GlobalBasePath , "/" ))))
155+ i .openSseServer = server .NewSSEServer (s , server .WithStaticBasePath (fmt .Sprintf ("/openapi/v1/%s" , strings .Trim (mcp_server .GlobalBasePath , "/" ))))
156+ i .openStreamableServer = server .NewStreamableHTTPServer (s , server .WithEndpointPath (mcp_server .OpenGlobalMCPPath ))
127157 }
128158 }
129159}
@@ -132,16 +162,16 @@ func (i *imlMcpController) GlobalMCPHandle(ctx *gin.Context) {
132162 cfg := i .settingModule .Get (ctx )
133163 req := ctx .Request .WithContext (utils .SetGatewayInvoke (ctx .Request .Context (), cfg .InvokeAddress ))
134164 locale := utils .I18n (ctx )
135- if v , ok := i .server [locale ]; ok {
165+ if v , ok := i .sseServers [locale ]; ok {
136166 v .ServeHTTP (ctx .Writer , req )
137167 return
138168 }
139- i .server [languageEnUs ].ServeHTTP (ctx .Writer , req )
169+ i .sseServers [languageEnUs ].ServeHTTP (ctx .Writer , req )
140170}
141171
142172func (i * imlMcpController ) GlobalHandleSSE (ctx * gin.Context ) {
143173 apikey := ctx .Request .URL .Query ().Get ("apikey" )
144- i .handleSSE (ctx , i .openServer , SessionInfo {
174+ i .handleSSE (ctx , i .openSseServer , SessionInfo {
145175 Apikey : apikey ,
146176 })
147177}
@@ -167,7 +197,16 @@ func (i *imlMcpController) handleSSE(ctx *gin.Context, server http.Handler, sIn
167197}
168198
169199func (i * imlMcpController ) GlobalHandleMessage (ctx * gin.Context ) {
170- i .handleMessage (ctx , i .openServer )
200+ i .handleMessage (ctx , i .openSseServer )
201+ }
202+
203+ func (i * imlMcpController ) GlobalHandleStreamHTTP (ctx * gin.Context ) {
204+ apikey := ctx .Request .Header .Get ("Authorization" )
205+ apikey = strings .TrimPrefix (apikey , "Bearer " )
206+ cfg := i .settingModule .Get (ctx )
207+ req := ctx .Request .WithContext (utils .SetGatewayInvoke (ctx .Request .Context (), cfg .InvokeAddress ))
208+ req = req .WithContext (utils .SetLabel (req .Context (), "apikey" , apikey ))
209+ i .openStreamableServer .ServeHTTP (ctx .Writer , req )
171210}
172211
173212func (i * imlMcpController ) MCPHandle (ctx * gin.Context ) {
@@ -204,12 +243,13 @@ func (i *imlMcpController) ServiceHandleMessage(ctx *gin.Context) {
204243}
205244
206245func (i * imlMcpController ) ServiceHandleStreamHTTP (ctx * gin.Context ) {
207- apikey := ctx .Request .URL . Query (). Get ("apikey " )
208- serviceId := ctx .Param ( "serviceId " )
246+ apikey := ctx .Request .Header . Get ("Authorization " )
247+ serviceId := ctx .Request . Header . Get ( "X-Service-Id " )
209248 if serviceId == "" {
210249 ctx .AbortWithStatusJSON (403 , gin.H {"code" : - 1 , "msg" : "invalid service id" , "success" : "fail" })
211250 return
212251 }
252+ apikey = strings .TrimPrefix (apikey , "Bearer " )
213253 ok , err := i .authorizationModule .CheckAPIKeyAuthorizationByService (ctx , serviceId , apikey )
214254 if err != nil {
215255 ctx .AbortWithStatusJSON (403 , gin.H {"code" : - 1 , "msg" : err .Error (), "success" : "fail" })
0 commit comments