modelcontextprotocol/typescript-sdk

Improvements to requestInfo

Opened this issue · 0 comments

Right now the requestInfo type is:

export type IsomorphicHeaders = Record<string, string | string[] | undefined>;
/**
 * Information about the incoming request.
 */
export interface RequestInfo {
    /**
     * The headers of the request.
     */
    headers: IsomorphicHeaders;
}

The benefit of this format is that it's serializable, but most of the time, consumers of this API don't need it to be serializeable and using it is a pain:

function getAuthHeader(requestInfo: RequestInfo) {
  const authHeader = requestInfo.headers.authorization
  if (!authHeader) return undefined
  if (Array.isArray(authHeader)) return authHeader[0]
  return authHeader
}

However, if it were changed to web a standard Headers object (which is supported in every relevant runtime):

/**
 * Information about the incoming request.
 */
export interface RequestInfo {
    /**
     * The headers of the request.
     */
    headers: Headers;
}

// ...

function getAuthHeader(requestInfo: RequestInfo) {
  return requestInfo.headers.get('authorization')
}

In addition, knowing the request url can be helpful as well:

/**
 * Information about the incoming request.
 */
export interface RequestInfo {
    /**
     * The headers of the request.
     */
    headers: Headers;
    /**
     * The URL of the request.
     */
    url: URL;
}

function shouldBustCache(requestInfo: RequestInfo) {
  return requestInfo.url.searchParams.get('forceFresh') === 'true'
}

So I propose the RequestInfo be changed to use a Headers object and provide the url of the request as well. I couldn't find in the spec where RequestInfo is actually specified, so I'm raising it here in the sdk to start things off.