@@ -20,12 +20,20 @@ import * as Context from "effect/Context";
2020import * as Deferred from "effect/Deferred" ;
2121import * as Effect from "effect/Effect" ;
2222import * as Layer from "effect/Layer" ;
23+ import * as Logger from "effect/Logger" ;
2324import * as Schema from "effect/Schema" ;
2425import * as Stream from "effect/Stream" ;
2526import { McpSchema , McpServer , Tool , Toolkit } from "effect/ai" ;
2627import { HttpBody , HttpClient , HttpClientRequest , HttpRouter } from "effect/http" ;
2728
2829import * as ProjectService from "../project/ProjectService.ts" ;
30+ import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts" ;
31+ import * as McpOAuth from "../auth/McpOAuth.ts" ;
32+ import * as ServerSecretStore from "../auth/ServerSecretStore.ts" ;
33+ import * as ServerEnvironment from "../environment/ServerEnvironment.ts" ;
34+ import * as Sqlite from "../persistence/Sqlite.ts" ;
35+ import * as ManagedPolicy from "../policy/ManagedPolicy.ts" ;
36+
2937import * as DeviceService from "../device/DeviceService.ts" ;
3038import * as PullRequestReviewGuideService from "../pullRequest/PullRequestReviewGuideService.ts" ;
3139import * as ServerConfig from "../config.ts" ;
@@ -946,6 +954,106 @@ it.effect("admits provider and OAuth client credentials and points only clients
946954 ) . pipe ( Effect . provide ( NodeHttpServer . layerTest ) ) ,
947955) ;
948956
957+ it . effect ( "accepts case-insensitive MCP bearer schemes and diagnoses rejected credentials" , ( ) => {
958+ const messages : Array < unknown > = [ ] ;
959+ const logger = Logger . make ( ( { message } ) => messages . push ( message ) ) ;
960+ const layerAuth = Layer . mergeAll ( EnvironmentAuth . layer , McpSessionRegistry . layer ) . pipe (
961+ Layer . provideMerge (
962+ Layer . mock ( ServerEnvironment . ServerEnvironment ) ( {
963+ getEnvironmentId : Effect . succeed ( environmentId ) ,
964+ } ) ,
965+ ) ,
966+ Layer . provideMerge ( Sqlite . layerMemory ) ,
967+ Layer . provide ( ServerSecretStore . layer ) ,
968+ Layer . provide ( ManagedPolicy . layerTest ( ) ) ,
969+ Layer . provideMerge ( ServerEnvironment . layerIdentity ) ,
970+ Layer . provide ( ServerConfig . layerTest ( process . cwd ( ) , { prefix : "t3-mcp-auth-header-test-" } ) ) ,
971+ Layer . provideMerge ( NodeServices . layer ) ,
972+ ) ;
973+ return Effect . scoped (
974+ Effect . gen ( function * ( ) {
975+ const auth = yield * EnvironmentAuth . EnvironmentAuth ;
976+ const registry = yield * McpSessionRegistry . McpSessionRegistry ;
977+ const issued = yield * registry . issue ( {
978+ threadId,
979+ providerInstanceId : ProviderInstanceId . make ( "codex" ) ,
980+ } ) ;
981+ const providerToken = issued . config . authorizationHeader . slice ( "Bearer " . length ) ;
982+ const oauth = yield * auth . issueMcpClientSession ( {
983+ label : "MCP header test" ,
984+ access : "read-only" ,
985+ peerLink : false ,
986+ client : { deviceType : "bot" } ,
987+ } ) ;
988+ yield * HttpRouter . serve (
989+ McpHttpServer . layerMcpTransport . pipe (
990+ Layer . provide ( McpOAuth . layerMcpClientAuthenticator ) ,
991+ Layer . provide ( ServerSettings . layerTest ( ) ) ,
992+ ) ,
993+ { disableListenLog : true , disableLogger : true } ,
994+ ) . pipe ( Layer . build ) ;
995+ const httpClient = yield * HttpClient . HttpClient ;
996+ const initialize = ( authorization ?: string ) =>
997+ httpClient . post ( "/mcp" , {
998+ headers : {
999+ accept : "application/json, text/event-stream" ,
1000+ ...( authorization === undefined ? { } : { authorization } ) ,
1001+ } ,
1002+ body : HttpBody . text (
1003+ `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"mcp-header-test","version":"1.0.0"}}}` ,
1004+ "application/json" ,
1005+ ) ,
1006+ } ) ;
1007+
1008+ for ( const scheme of [ "Bearer" , "bearer" , "bEaReR" ] ) {
1009+ for ( const [ kind , token ] of [
1010+ [ "provider" , providerToken ] ,
1011+ [ "OAuth" , oauth . token ] ,
1012+ ] ) {
1013+ const response = yield * initialize ( `${ scheme } ${ token } ` ) ;
1014+ expect . soft ( response . status , `${ kind } ${ scheme } ` ) . toBe ( 200 ) ;
1015+ }
1016+ }
1017+ messages . length = 0 ;
1018+ for ( const [ authorization , reason ] of [
1019+ [ undefined , "missing_authorization_header" ] ,
1020+ [ `Basic ${ providerToken } ` , "unsupported_scheme" ] ,
1021+ [ "Bearer" , "missing_bearer_token" ] ,
1022+ [ "bEaReR " , "missing_bearer_token" ] ,
1023+ [ `bearer ${ providerToken . toUpperCase ( ) } ` , "unknown_or_expired_token" ] ,
1024+ [ `bEaReR ${ oauth . token . toUpperCase ( ) } ` , "unknown_or_expired_token" ] ,
1025+ ] as const ) {
1026+ const response = yield * initialize ( authorization ) ;
1027+ expect ( response . status ) . toBe ( 401 ) ;
1028+ expect
1029+ . soft ( messages . at ( - 1 ) )
1030+ . toEqual ( [ "rejected MCP request with an unusable credential" , { reason } ] ) ;
1031+ const body = yield * response . text ;
1032+ expect ( body ) . not . toContain ( providerToken ) ;
1033+ expect ( body ) . not . toContain ( oauth . token ) ;
1034+ expect ( JSON . stringify ( response . headers ) ) . not . toContain ( providerToken ) ;
1035+ expect ( JSON . stringify ( response . headers ) ) . not . toContain ( oauth . token ) ;
1036+ }
1037+ yield * registry . revokeThread ( threadId ) ;
1038+ yield * auth . revokeSession ( oauth . sessionId ) ;
1039+ for ( const token of [ providerToken , oauth . token ] ) {
1040+ expect ( ( yield * initialize ( `bearer ${ token } ` ) ) . status ) . toBe ( 401 ) ;
1041+ }
1042+ expect ( JSON . stringify ( messages ) ) . not . toContain ( providerToken ) ;
1043+ expect ( JSON . stringify ( messages ) ) . not . toContain ( oauth . token ) ;
1044+ expect ( JSON . stringify ( messages ) ) . not . toContain ( providerToken . toUpperCase ( ) ) ;
1045+ expect ( JSON . stringify ( messages ) ) . not . toContain ( oauth . token . toUpperCase ( ) ) ;
1046+ } ) ,
1047+ ) . pipe (
1048+ Effect . provide (
1049+ layerAuth . pipe (
1050+ Layer . provideMerge ( NodeHttpServer . layerTest ) ,
1051+ Layer . provideMerge ( Logger . layer ( [ logger ] , { mergeWithExisting : false } ) ) ,
1052+ ) ,
1053+ ) ,
1054+ ) ;
1055+ } ) ;
1056+
9491057it . effect ( "hides tools for links and refuses another environment while the beta is off" , ( ) =>
9501058 Effect . scoped (
9511059 Effect . gen ( function * ( ) {
0 commit comments