@@ -2,6 +2,7 @@ import { createMcpHandler } from 'mcp-handler'
22import * as cheerio from 'cheerio'
33import { z } from 'zod'
44import type { Entries } from 'type-fest'
5+ import { headers } from 'next/headers'
56import { libs , SUPPORTED_LIBRARY_NAMES } from '@/app/page'
67
78// Extract entries and library names as constants for efficiency
@@ -11,15 +12,15 @@ const libsEntries = (Object.entries(libs) as Entries<typeof libs>).filter(
1112 ( [ , lib ] ) => lib . docs_url . includes ( 'pmndrs.github.io' ) || lib . docs_url . startsWith ( '/' ) ,
1213)
1314
14- function toAbsoluteUrl ( url : string ) {
15- // Use Vercel URL in production, fallback to NEXT_PUBLIC_URL otherwise
16- console . log ( 'VERCEL_URL:' , process . env . VERCEL_URL )
17- console . log ( 'NEXT_PUBLIC_URL:' , process . env . NEXT_PUBLIC_URL )
18- const baseUrl = process . env . VERCEL_URL
19- ? `https:// ${ process . env . VERCEL_URL } `
20- : ( process . env . NEXT_PUBLIC_URL ?? '' )
21- console . log ( 'baseUrl :' , baseUrl , 'url:' , url )
22- return `${ baseUrl } ${ url } `
15+ async function toAbsoluteUrl ( url : string ) {
16+ // Try VERCEL_URL env var first, then get from request headers
17+ const host = process . env . VERCEL_URL || ( await headers ( ) ) . get ( 'host' )
18+ if ( ! host ) throw new Error ( 'Unable to determine host' )
19+ const protocol = host . includes ( 'localhost' ) ? 'http' : 'https'
20+
21+ console . log ( 'protocol:' , protocol )
22+ console . log ( 'host :' , host )
23+ return `${ protocol } :// ${ host } ${ url } `
2324}
2425
2526const handler = createMcpHandler (
@@ -34,7 +35,8 @@ const handler = createMcpHandler(
3435 } ,
3536 async ( ) => {
3637 // Fetch the skill.md file hosted on docs.pmnd.rs
37- const content = await fetch ( 'https://docs.pmnd.rs/skill.md' ) . then ( ( r ) => r . text ( ) )
38+ const url = await toAbsoluteUrl ( '/skill.md' )
39+ const content = await fetch ( url ) . then ( ( r ) => r . text ( ) )
3840 return {
3941 contents : [
4042 {
@@ -59,7 +61,7 @@ const handler = createMcpHandler(
5961 let url : string = lib . docs_url
6062
6163 if ( url . startsWith ( '/' ) ) {
62- url = toAbsoluteUrl ( url )
64+ url = await toAbsoluteUrl ( url )
6365 }
6466
6567 // Fetch the remote file
@@ -102,7 +104,7 @@ const handler = createMcpHandler(
102104 let url : string = libs [ lib ] . docs_url
103105
104106 if ( url . startsWith ( '/' ) ) {
105- url = toAbsoluteUrl ( url )
107+ url = await toAbsoluteUrl ( url )
106108 }
107109
108110 try {
0 commit comments