@@ -28,8 +28,17 @@ app.use(
2828 "*" ,
2929 cors ( {
3030 origin : "*" ,
31- allowMethods : [ "GET" , "POST" , "OPTIONS" ] ,
32- allowHeaders : [ "Content-Type" , "Authorization" , "x-sm-project" ] ,
31+ allowMethods : [ "GET" , "POST" , "DELETE" , "OPTIONS" ] ,
32+ allowHeaders : [
33+ "Content-Type" ,
34+ "Authorization" ,
35+ "x-sm-project" ,
36+ "Accept" ,
37+ "Mcp-Session-Id" ,
38+ "MCP-Protocol-Version" ,
39+ "Last-Event-ID" ,
40+ ] ,
41+ exposeHeaders : [ "Mcp-Session-Id" , "WWW-Authenticate" ] ,
3342 } ) ,
3443)
3544
@@ -50,10 +59,9 @@ app.get("/", (c) => {
5059// MCP clients use this to discover the authorization server
5160app . get ( "/.well-known/oauth-protected-resource" , ( c ) => {
5261 const apiUrl = c . env . API_URL || DEFAULT_API_URL
53- const resourceUrl =
54- c . env . API_URL === "http://localhost:8787"
55- ? "http://localhost:8788"
56- : "https://mcp.supermemory.ai"
62+ const host = c . req . header ( "x-forwarded-host" ) || c . req . header ( "host" )
63+ const proto = c . req . header ( "x-forwarded-proto" ) || "https"
64+ const resourceUrl = host ? `${ proto } ://${ host } ` : "https://mcp.supermemory.ai"
5765
5866 return c . json ( {
5967 resource : resourceUrl ,
@@ -91,12 +99,13 @@ app.get("/.well-known/oauth-authorization-server", async (c) => {
9199 }
92100} )
93101
94- const mcpHandler = SupermemoryMCP . mount ( "/mcp" , {
102+ const mcpHandler = SupermemoryMCP . serve ( "/mcp" , {
95103 binding : "MCP_SERVER" ,
96104 corsOptions : {
97105 origin : "*" ,
98- methods : "GET, POST, OPTIONS" ,
99- headers : "Content-Type, Authorization, x-sm-project" ,
106+ methods : "GET, POST, DELETE, OPTIONS" ,
107+ headers :
108+ "Content-Type, Authorization, x-sm-project, Accept, Mcp-Session-Id, MCP-Protocol-Version, Last-Event-ID" ,
100109 } ,
101110} )
102111
@@ -105,17 +114,20 @@ app.all("/mcp/*", async (c) => {
105114 const token = authHeader ?. replace ( / ^ B e a r e r \s + / i, "" )
106115 const containerTag = c . req . header ( "x-sm-project" )
107116 const apiUrl = c . env . API_URL || DEFAULT_API_URL
108- const mcpURL =
109- c . env . API_URL === "http://localhost:8787"
110- ? "http://localhost:8788"
111- : "https://mcp.supermemory.ai"
117+
118+ const reqHost = c . req . header ( "x-forwarded-host" ) || c . req . header ( "host" ) || ""
119+ const reqProto = c . req . header ( "x-forwarded-proto" ) || "https"
120+ const resourceMetadataUrl = reqHost
121+ ? `${ reqProto } ://${ reqHost } /.well-known/oauth-protected-resource`
122+ : "/.well-known/oauth-protected-resource"
112123
113124 if ( ! token ) {
114125 return new Response ( "Unauthorized" , {
115126 status : 401 ,
116127 headers : {
117- "WWW-Authenticate" : `Bearer resource_metadata="${ mcpURL } /.well-known/oauth-protected-resource "` ,
128+ "WWW-Authenticate" : `Bearer resource_metadata="${ resourceMetadataUrl } "` ,
118129 "Access-Control-Expose-Headers" : "WWW-Authenticate" ,
130+ "Access-Control-Allow-Origin" : "*" ,
119131 } ,
120132 } )
121133 }
@@ -153,8 +165,9 @@ app.all("/mcp/*", async (c) => {
153165 status : 401 ,
154166 headers : {
155167 "Content-Type" : "application/json" ,
156- "WWW-Authenticate" : `Bearer error="invalid_token", resource_metadata="${ mcpURL } /.well-known/oauth-protected-resource "` ,
168+ "WWW-Authenticate" : `Bearer error="invalid_token", resource_metadata="${ resourceMetadataUrl } "` ,
157169 "Access-Control-Expose-Headers" : "WWW-Authenticate" ,
170+ "Access-Control-Allow-Origin" : "*" ,
158171 } ,
159172 } ,
160173 )
0 commit comments