55 */
66
77import assert from 'node:assert' ;
8+ import os from 'node:os' ;
89import path from 'node:path' ;
910import { afterEach , describe , it } from 'node:test' ;
1011import { pathToFileURL } from 'node:url' ;
@@ -220,13 +221,21 @@ describe('McpContext', () => {
220221 await withMcpContext ( async ( _response , context ) => {
221222 const roots = [ { uri : 'file:///test' , name : 'test' } ] ;
222223 context . setRoots ( roots ) ;
223- assert . deepEqual ( context . roots ( ) , roots ) ;
224+ const actualRoots = context . roots ( ) ;
225+ assert . ok (
226+ actualRoots ?. some ( r => r . name === 'test' ) ,
227+ 'Should contain the set root' ,
228+ ) ;
229+ assert . ok (
230+ actualRoots ?. some ( r => r . name === 'temp' ) ,
231+ 'Should contain the temp root' ,
232+ ) ;
224233 } ) ;
225234 } ) ;
226235
227236 it ( 'validatePath allows paths within roots' , async ( ) => {
228237 await withMcpContext ( async ( _response , context ) => {
229- const workspacePath = path . resolve ( '/tmp/ workspace') ;
238+ const workspacePath = path . resolve ( os . homedir ( ) , ' workspace-test ') ;
230239 const roots = [
231240 { uri : pathToFileURL ( workspacePath ) . href , name : 'workspace' } ,
232241 ] ;
@@ -235,24 +244,28 @@ describe('McpContext', () => {
235244 context . validatePath ( path . join ( workspacePath , 'test.txt' ) ) ;
236245 context . validatePath ( workspacePath ) ;
237246
238- // Invalid path outside root
239- const outsidePath = path . resolve ( '/tmp/ outside.txt') ;
247+ // Invalid path outside root and outside temp dir
248+ const outsidePath = path . resolve ( os . homedir ( ) , ' outside-test .txt') ;
240249 assert . throws ( ( ) => context . validatePath ( outsidePath ) , / A c c e s s d e n i e d / ) ;
241250 } ) ;
242251 } ) ;
243252
244253 it ( 'validatePath allows all paths if roots are undefined (legacy)' , async ( ) => {
245254 await withMcpContext ( async ( _response , context ) => {
246255 context . setRoots ( undefined ) ;
247- context . validatePath ( path . resolve ( '/tmp/ anywhere.txt') ) ;
256+ context . validatePath ( path . resolve ( os . homedir ( ) , ' anywhere.txt') ) ;
248257 } ) ;
249258 } ) ;
250259
251- it ( 'validatePath denies all paths if roots list is empty' , async ( ) => {
260+ it ( 'validatePath denies paths outside os.tmpdir() if roots list is empty' , async ( ) => {
252261 await withMcpContext ( async ( _response , context ) => {
253262 context . setRoots ( [ ] ) ;
263+ // Should allow temp dir
264+ context . validatePath ( path . join ( os . tmpdir ( ) , 'test.txt' ) ) ;
265+
266+ // Should deny outside temp dir
254267 assert . throws (
255- ( ) => context . validatePath ( path . resolve ( '/tmp/ anywhere.txt') ) ,
268+ ( ) => context . validatePath ( path . resolve ( os . homedir ( ) , ' anywhere.txt') ) ,
256269 / A c c e s s d e n i e d / ,
257270 ) ;
258271 } ) ;
0 commit comments