|
| 1 | +(* ::Section::Closed:: *) |
| 2 | +(*Package Header*) |
| 3 | +BeginPackage[ "Wolfram`AgentTools`MCPRoots`" ]; |
| 4 | +Begin[ "`Private`" ]; |
| 5 | + |
| 6 | +Needs[ "Wolfram`AgentTools`" ]; |
| 7 | +Needs[ "Wolfram`AgentTools`Common`" ]; |
| 8 | + |
| 9 | +(* ::**************************************************************************************************************:: *) |
| 10 | +(* ::Section::Closed:: *) |
| 11 | +(*Session State*) |
| 12 | +$clientSupportsRoots = False; |
| 13 | +$mcpRoot = None; |
| 14 | + |
| 15 | +(* ::**************************************************************************************************************:: *) |
| 16 | +(* ::Section::Closed:: *) |
| 17 | +(*onClientInitialized*) |
| 18 | +onClientInitialized // beginDefinition; |
| 19 | + |
| 20 | +onClientInitialized[ _ ] := |
| 21 | + If[ TrueQ @ $clientSupportsRoots, |
| 22 | + sendClientRequest[ "roots/list", <| |>, handleRootsListResponse ] |
| 23 | + ]; |
| 24 | + |
| 25 | +onClientInitialized // endDefinition; |
| 26 | + |
| 27 | +(* ::**************************************************************************************************************:: *) |
| 28 | +(* ::Section::Closed:: *) |
| 29 | +(*onRootsListChanged*) |
| 30 | +onRootsListChanged // beginDefinition; |
| 31 | + |
| 32 | +onRootsListChanged[ _ ] := |
| 33 | + If[ TrueQ @ $clientSupportsRoots, |
| 34 | + sendClientRequest[ "roots/list", <| |>, handleRootsListResponse ] |
| 35 | + ]; |
| 36 | + |
| 37 | +onRootsListChanged // endDefinition; |
| 38 | + |
| 39 | +(* ::**************************************************************************************************************:: *) |
| 40 | +(* ::Section::Closed:: *) |
| 41 | +(*handleRootsListResponse*) |
| 42 | +handleRootsListResponse // beginDefinition; |
| 43 | + |
| 44 | +handleRootsListResponse[ request_, response_Association ] := |
| 45 | + Catch @ Module[ { roots, root }, |
| 46 | + If[ KeyExistsQ[ response, "error" ], |
| 47 | + writeLog[ "RootsListError" -> response[ "error" ] ]; |
| 48 | + Throw @ Null |
| 49 | + ]; |
| 50 | + roots = response[ "result", "roots" ]; |
| 51 | + root = pickFirstValidRoot @ roots; |
| 52 | + If[ StringQ @ root, |
| 53 | + applyMCPRoot @ root, |
| 54 | + writeLog[ "RootsListEmptyOrInvalid" -> roots ] |
| 55 | + ] |
| 56 | + ]; |
| 57 | + |
| 58 | +handleRootsListResponse // endDefinition; |
| 59 | + |
| 60 | +(* ::**************************************************************************************************************:: *) |
| 61 | +(* ::Section::Closed:: *) |
| 62 | +(*pickFirstValidRoot*) |
| 63 | +pickFirstValidRoot // beginDefinition; |
| 64 | + |
| 65 | +pickFirstValidRoot[ roots_List ] := |
| 66 | + SelectFirst[ |
| 67 | + rootURIToPath /@ Cases[ roots, KeyValuePattern[ "uri" -> uri_String ] :> uri ], |
| 68 | + StringQ[ # ] && DirectoryQ[ # ] &, |
| 69 | + None |
| 70 | + ]; |
| 71 | + |
| 72 | +pickFirstValidRoot[ _ ] := None; |
| 73 | + |
| 74 | +pickFirstValidRoot // endDefinition; |
| 75 | + |
| 76 | +(* ::**************************************************************************************************************:: *) |
| 77 | +(* ::Section::Closed:: *) |
| 78 | +(*rootURIToPath*) |
| 79 | +rootURIToPath // beginDefinition; |
| 80 | +rootURIToPath[ uri_String /; StringStartsQ[ uri, "file://" ] ] := ExpandFileName @ LocalObject @ normalizeFileURI @ uri; |
| 81 | +rootURIToPath[ _ ] := None; |
| 82 | +rootURIToPath // endDefinition; |
| 83 | + |
| 84 | +(* ::**************************************************************************************************************:: *) |
| 85 | +(* ::Section::Closed:: *) |
| 86 | +(*normalizeFileURI*) |
| 87 | +(* Some clients (e.g. Claude Code on Windows) emit malformed file:// URIs containing |
| 88 | + backslashes and only two slashes before a drive letter, like |
| 89 | + "file://H:\\Documents\\AgentTools". Convert those to a well-formed |
| 90 | + "file:///H:/Documents/AgentTools" so LocalObject can decode them correctly. *) |
| 91 | +normalizeFileURI // beginDefinition; |
| 92 | + |
| 93 | +normalizeFileURI[ uri_String ] := |
| 94 | + StringReplace[ |
| 95 | + StringReplace[ uri, "\\" -> "/" ], |
| 96 | + StartOfString ~~ "file://" ~~ c: LetterCharacter ~~ ":/" :> "file:///" <> c <> ":/" |
| 97 | + ]; |
| 98 | + |
| 99 | +normalizeFileURI // endDefinition; |
| 100 | + |
| 101 | +(* ::**************************************************************************************************************:: *) |
| 102 | +(* ::Section::Closed:: *) |
| 103 | +(*applyMCPRoot*) |
| 104 | +applyMCPRoot // beginDefinition; |
| 105 | + |
| 106 | +applyMCPRoot[ root_String ] := ( |
| 107 | + $mcpRoot = root; |
| 108 | + SetDirectory @ root; |
| 109 | + If[ toolOptionValue[ "WolframLanguageEvaluator", "Method" ] === "Local", |
| 110 | + useEvaluatorKernel @ SetDirectory @ root |
| 111 | + ]; |
| 112 | + writeLog[ "RootApplied" -> root ]; |
| 113 | +); |
| 114 | + |
| 115 | +applyMCPRoot // endDefinition; |
| 116 | + |
| 117 | +(* ::**************************************************************************************************************:: *) |
| 118 | +(* ::Section::Closed:: *) |
| 119 | +(*Package Footer*) |
| 120 | +addToMXInitialization[ |
| 121 | + $clientSupportsRoots = False; |
| 122 | + $mcpRoot = None; |
| 123 | +]; |
| 124 | + |
| 125 | +End[ ]; |
| 126 | +EndPackage[ ]; |
0 commit comments