2222 * injected registry when the scope is absent, and the long-lived stdio
2323 * bridge (`bridgeTools`) still calls its registry with ONE argument — the
2424 * shared registration body gained a seam, not a behaviour change.
25+ * 4. [#31022] The INSTRUCTIONS. A host's per-request instructions on the
26+ * injection answer `initialize` only under the grant that registers the
27+ * family; a `data:read` token, a key or a session gets the configured or
28+ * default text, and so does an authoring request that carries none.
2529 */
2630
2731import { describe , it , expect , beforeEach , afterEach } from 'vitest' ;
@@ -141,6 +145,8 @@ interface CallOptions {
141145 headers ?: Record < string , string > ;
142146 /** Omit the data bridge entirely (authoring-only host). */
143147 noBridge ?: boolean ;
148+ /** [#31022] Per-request server instructions carried on the authoring injection. */
149+ instructions ?: string ;
144150}
145151
146152async function call ( runtime : MCPServerRuntime , bridge : any , body : unknown , o : CallOptions = { } ) {
@@ -158,7 +164,14 @@ async function call(runtime: MCPServerRuntime, bridge: any, body: unknown, o: Ca
158164 parsedBody : body ,
159165 ...( o . grantedScopes ? { toolOptions : { grantedScopes : o . grantedScopes } } : { } ) ,
160166 ...( o . registry
161- ? { authoring : { registry : o . registry , principal : PRINCIPAL , environmentId : ENVIRONMENT_ID } }
167+ ? {
168+ authoring : {
169+ registry : o . registry ,
170+ principal : PRINCIPAL ,
171+ environmentId : ENVIRONMENT_ID ,
172+ ...( o . instructions !== undefined ? { instructions : o . instructions } : { } ) ,
173+ } ,
174+ }
162175 : { } ) ,
163176 } ) ;
164177 // The buffered JSON-RPC answer, read untyped on purpose: these pins assert
@@ -603,3 +616,92 @@ describe('[#26464] stdio parity — bridgeTools still calls its ToolRegistry wit
603616 expect ( arities ) . toEqual ( [ 1 ] ) ;
604617 } ) ;
605618} ) ;
619+
620+ describe ( '[#31022] per-request instructions on the authoring injection — answered at initialize' , ( ) => {
621+ let runtime : MCPServerRuntime ;
622+ let bridge : ReturnType < typeof makeBridge > ;
623+ let registry : RecordingAuthoringRegistry ;
624+ const HOST_INSTRUCTIONS = 'Authoring rules: call get_authoring_rules with no arguments for the index.' ;
625+
626+ beforeEach ( ( ) => {
627+ runtime = new MCPServerRuntime ( { name : 't' , version : '1.0.0' } ) ;
628+ bridge = makeBridge ( ) ;
629+ registry = makeAuthoringRegistry ( ) ;
630+ } ) ;
631+
632+ async function instructionsAtInitialize ( rt : MCPServerRuntime , o : CallOptions = { } ) : Promise < unknown > {
633+ const { status, json } = await call ( rt , bridge , {
634+ jsonrpc : '2.0' ,
635+ id : 1 ,
636+ method : 'initialize' ,
637+ params : { protocolVersion : '2025-06-18' , capabilities : { } , clientInfo : { name : 'pin' , version : '0' } } ,
638+ } , o ) ;
639+ expect ( status ) . toBe ( 200 ) ;
640+ expect ( json . error ) . toBeUndefined ( ) ;
641+ return json . result . instructions ;
642+ }
643+
644+ it ( 'a metadata:write request whose injection carries instructions is answered with them' , async ( ) => {
645+ const control = await instructionsAtInitialize ( runtime , { grantedScopes : [ MCP_OAUTH_SCOPE_METADATA_WRITE ] , registry } ) ;
646+ const subject = await instructionsAtInitialize ( runtime , {
647+ grantedScopes : [ MCP_OAUTH_SCOPE_DATA_READ , MCP_OAUTH_SCOPE_METADATA_WRITE ] ,
648+ registry,
649+ instructions : HOST_INSTRUCTIONS ,
650+ } ) ;
651+ expect ( subject ) . toBe ( HOST_INSTRUCTIONS ) ;
652+ // Control leg: the same door without the field answers something else, so
653+ // the equality above is the injection's doing and not a coincidence.
654+ expect ( control ) . not . toBe ( HOST_INSTRUCTIONS ) ;
655+ } ) ;
656+
657+ it ( 'a data:read request never sees the injected text — it gets the default' , async ( ) => {
658+ const defaultText = await instructionsAtInitialize ( runtime , { grantedScopes : [ MCP_OAUTH_SCOPE_DATA_READ ] } ) ;
659+ const subject = await instructionsAtInitialize ( runtime , {
660+ grantedScopes : [ MCP_OAUTH_SCOPE_DATA_READ ] ,
661+ registry,
662+ instructions : HOST_INSTRUCTIONS ,
663+ } ) ;
664+ expect ( typeof defaultText ) . toBe ( 'string' ) ;
665+ expect ( subject ) . toBe ( defaultText ) ;
666+ expect ( subject ) . not . toBe ( HOST_INSTRUCTIONS ) ;
667+ } ) ;
668+
669+ it ( 'the undefined grant (API key / session) and a data:write token never see it either' , async ( ) => {
670+ const defaultText = await instructionsAtInitialize ( runtime , { } ) ;
671+ expect ( await instructionsAtInitialize ( runtime , { registry, instructions : HOST_INSTRUCTIONS } ) ) . toBe ( defaultText ) ;
672+ expect (
673+ await instructionsAtInitialize ( runtime , {
674+ grantedScopes : [ MCP_OAUTH_SCOPE_DATA_WRITE ] ,
675+ registry,
676+ instructions : HOST_INSTRUCTIONS ,
677+ } ) ,
678+ ) . toBe ( defaultText ) ;
679+ } ) ;
680+
681+ it ( 'a metadata:write request whose injection carries no instructions gets the default' , async ( ) => {
682+ const defaultText = await instructionsAtInitialize ( runtime , { grantedScopes : [ MCP_OAUTH_SCOPE_DATA_READ ] } ) ;
683+ const subject = await instructionsAtInitialize ( runtime , { grantedScopes : [ MCP_OAUTH_SCOPE_METADATA_WRITE ] , registry } ) ;
684+ expect ( subject ) . toBe ( defaultText ) ;
685+ } ) ;
686+
687+ it ( 'a runtime configured with instructions keeps them for every request the injection does not reach' , async ( ) => {
688+ const CONFIGURED = 'Configured server-wide instructions.' ;
689+ const configured = new MCPServerRuntime ( { name : 't' , version : '1.0.0' , instructions : CONFIGURED } ) ;
690+ expect (
691+ await instructionsAtInitialize ( configured , {
692+ grantedScopes : [ MCP_OAUTH_SCOPE_DATA_READ ] ,
693+ registry,
694+ instructions : HOST_INSTRUCTIONS ,
695+ } ) ,
696+ ) . toBe ( CONFIGURED ) ;
697+ expect ( await instructionsAtInitialize ( configured , { grantedScopes : [ MCP_OAUTH_SCOPE_METADATA_WRITE ] , registry } ) ) . toBe ( CONFIGURED ) ;
698+ // Under the admitting grant the per-request text wins over the server-wide one.
699+ expect (
700+ await instructionsAtInitialize ( configured , {
701+ grantedScopes : [ MCP_OAUTH_SCOPE_METADATA_WRITE ] ,
702+ registry,
703+ instructions : HOST_INSTRUCTIONS ,
704+ } ) ,
705+ ) . toBe ( HOST_INSTRUCTIONS ) ;
706+ } ) ;
707+ } ) ;
0 commit comments