Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,15 @@ async function checkToken(token: string, baseUrl: string): Promise<Response | 'p
error: forbidden ? 'insufficient_scope' : undefined,
resourceMetadataUrl:
'https://notes.example.com/.well-known/oauth-protected-resource/mcp',
scopes: ['notes:read', 'notes:write'],
}),
},
})
}
```

- `isTransientValidationError`: a timeout, a dropped or refused connection, or a 5xx, the rejections worth retrying. A redirect or a 404 is not.
- `buildBearerChallenge`: `Bearer`, with `error="insufficient_scope"` and `resource_metadata="…"` when given.
- `buildBearerChallenge`: `Bearer`, with `error="insufficient_scope"`, `resource_metadata="…"` and `scope="…"` (from `scopes`, space-separated) when given. MCP clients request the challenge's `scope` ahead of the metadata's `scopes_supported`, so pass the resource's scopes to help a client that never reads the metadata.
- `createTokenVerdictCache`: verdicts keyed by token and base URL, kept for `ttlMs`, the oldest dropped when full.

### Contract tests
Expand Down
11 changes: 11 additions & 0 deletions src/host/challenge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest'
import { buildBearerChallenge } from './challenge.js'

const METADATA = 'https://mcp.example.com/.well-known/oauth-protected-resource/mcp'
const SCOPES = ['notes:read', 'notes:write']

describe('buildBearerChallenge', () => {
it.each([
Expand All @@ -12,6 +13,16 @@ describe('buildBearerChallenge', () => {
{ error: 'insufficient_scope' as const, resourceMetadataUrl: METADATA },
`Bearer error="insufficient_scope", resource_metadata="${METADATA}"`,
],
[{ scopes: SCOPES }, 'Bearer scope="notes:read notes:write"'],
[
{ resourceMetadataUrl: METADATA, scopes: SCOPES },
`Bearer resource_metadata="${METADATA}", scope="notes:read notes:write"`,
],
[
{ error: 'insufficient_scope' as const, resourceMetadataUrl: METADATA, scopes: SCOPES },
`Bearer error="insufficient_scope", resource_metadata="${METADATA}", scope="notes:read notes:write"`,
],
[{ resourceMetadataUrl: METADATA, scopes: [] }, `Bearer resource_metadata="${METADATA}"`],
])('builds the challenge for %o', (options, expected) => {
expect(buildBearerChallenge(options)).toBe(expected)
})
Expand Down
8 changes: 8 additions & 0 deletions src/host/challenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ export type BearerChallengeOptions = {
error?: 'insufficient_scope'
/** The resource's protected-resource metadata (RFC 9728), where the client finds how to authorise. */
resourceMetadataUrl?: string
/**
* The scopes a token needs for this resource, sent as RFC 6750's `scope` attribute. MCP clients
* request these first, ahead of the metadata's `scopes_supported`, so a client that can't find
* the resource's metadata still asks for the right ones. Left out when empty.
*/
scopes?: readonly string[]
}

/** The `WWW-Authenticate` value for a refused bearer token. */
export function buildBearerChallenge({
error,
resourceMetadataUrl,
scopes,
}: BearerChallengeOptions = {}): string {
const params = [
...(error ? [`error="${error}"`] : []),
...(resourceMetadataUrl ? [`resource_metadata="${resourceMetadataUrl}"`] : []),
...(scopes && scopes.length > 0 ? [`scope="${scopes.join(' ')}"`] : []),
]
return params.length > 0 ? `Bearer ${params.join(', ')}` : 'Bearer'
}
Loading