2323from starlette .routing import Mount , Route
2424
2525from splunklib .ai import Agent
26+ from splunklib .ai .agent import (
27+ _get_splunk_username , # pyright: ignore[reportPrivateUsage]
28+ )
2629from splunklib .ai .engines .langchain import LOCAL_TOOL_PREFIX
2730from splunklib .ai .messages import (
2831 AIMessage ,
5053)
5154from splunklib .ai .tools import (
5255 ToolType ,
53- _get_splunk_username , # pyright: ignore[reportPrivateUsage]
5456 locate_app ,
5557)
5658from splunklib .client import connect
@@ -296,6 +298,12 @@ async def mcp_token_handler(_: Request) -> Response:
296298 return JSONResponse (content = {"token" : AUTH_TOKEN }, status_code = 200 )
297299
298300
301+ async def current_context_handler (_ : Request ) -> Response :
302+ return JSONResponse (
303+ content = {"entry" : [{"content" : {"username" : "admin" }}]}, status_code = 200
304+ )
305+
306+
299307class TestRemoteTools (AITestCase ):
300308 @patch (
301309 "splunklib.ai.agent._testing_local_tools_path" ,
@@ -364,6 +372,11 @@ async def dispatch(
364372 routes = [
365373 Mount ("/services/mcp" , app = mcp .streamable_http_app ()),
366374 Route ("/services/mcp_token" , mcp_token_handler , methods = ["GET" ]),
375+ Route (
376+ "/services/authentication/current-context" ,
377+ current_context_handler ,
378+ methods = ["GET" ],
379+ ),
367380 ],
368381 lifespan = lifespan ,
369382 middleware = [Middleware (MCPMiddleware )],
@@ -376,7 +389,6 @@ async def dispatch(
376389 port = port ,
377390 splunkToken = AUTH_TOKEN ,
378391 autologin = True ,
379- username = "admin" , # not required, but set to avoid mocking the authentication/current-context endpoint
380392 ),
381393 )
382394
@@ -427,15 +439,24 @@ async def dispatch(
427439 async def test_remote_tools_mcp_app_unavailable (self ) -> None :
428440 pytest .importorskip ("langchain_openai" )
429441
430- async with run_http_server (Starlette (routes = [])) as (host , port ):
442+ async with run_http_server (
443+ Starlette (
444+ routes = [
445+ Route (
446+ "/services/authentication/current-context" ,
447+ current_context_handler ,
448+ methods = ["GET" ],
449+ ),
450+ ]
451+ )
452+ ) as (host , port ):
431453 service = await asyncio .to_thread (
432454 lambda : connect (
433455 scheme = "http" ,
434456 host = host ,
435457 port = port ,
436458 splunkToken = AUTH_TOKEN ,
437459 autologin = True ,
438- username = "admin" , # not required, but set to avoid mocking the authentication/current-context endpoint
439460 ),
440461 )
441462
@@ -489,6 +510,11 @@ async def lifespan(app: Starlette) -> AsyncGenerator[None, Any]:
489510 routes = [
490511 Mount ("/services/mcp" , app = mcp .streamable_http_app ()),
491512 Route ("/services/mcp_token" , mcp_token_handler , methods = ["GET" ]),
513+ Route (
514+ "/services/authentication/current-context" ,
515+ current_context_handler ,
516+ methods = ["GET" ],
517+ ),
492518 ],
493519 lifespan = lifespan ,
494520 )
@@ -500,7 +526,6 @@ async def lifespan(app: Starlette) -> AsyncGenerator[None, Any]:
500526 port = port ,
501527 splunkToken = AUTH_TOKEN ,
502528 autologin = True ,
503- username = "admin" , # not required, but set to avoid mocking the authentication/current-context endpoint
504529 ),
505530 )
506531
@@ -579,6 +604,11 @@ async def lifespan(app: Starlette) -> AsyncGenerator[None, Any]:
579604 routes = [
580605 Mount ("/services/mcp" , app = mcp .streamable_http_app ()),
581606 Route ("/services/mcp_token" , mcp_token_handler , methods = ["GET" ]),
607+ Route (
608+ "/services/authentication/current-context" ,
609+ current_context_handler ,
610+ methods = ["GET" ],
611+ ),
582612 ],
583613 lifespan = lifespan ,
584614 )
@@ -590,7 +620,6 @@ async def lifespan(app: Starlette) -> AsyncGenerator[None, Any]:
590620 port = port ,
591621 splunkToken = AUTH_TOKEN ,
592622 autologin = True ,
593- username = "admin" , # not required, but set to avoid mocking the authentication/current-context endpoint
594623 ),
595624 )
596625
@@ -732,6 +761,11 @@ async def lifespan(_app: Starlette) -> AsyncGenerator[None, Any]:
732761 routes = [
733762 Mount ("/services/mcp" , app = mcp .streamable_http_app ()),
734763 Route ("/services/mcp_token" , mcp_token_handler , methods = ["GET" ]),
764+ Route (
765+ "/services/authentication/current-context" ,
766+ current_context_handler ,
767+ methods = ["GET" ],
768+ ),
735769 ],
736770 lifespan = lifespan ,
737771 )
@@ -743,8 +777,6 @@ async def lifespan(_app: Starlette) -> AsyncGenerator[None, Any]:
743777 port = port ,
744778 splunkToken = AUTH_TOKEN ,
745779 autologin = True ,
746- # To avoid mocking `authentication/current-context` endpoint
747- username = "admin" ,
748780 ),
749781 )
750782
0 commit comments