diff --git a/docs/user/building-on-mcp.md b/docs/user/building-on-mcp.md index c9b70d3f..59a16ec8 100644 --- a/docs/user/building-on-mcp.md +++ b/docs/user/building-on-mcp.md @@ -434,6 +434,26 @@ A tool from outside MCP is registered and discoverable, not advertised, so it costs nothing in the client's context until something looks for it. Answer `'static'` from `defaultExposure` to be in `tools/list` from the start. +A user moves tools across those defaults for every server they run, whatever +started its image, in `tools.json` in MCP's folder of their configuration: +`$XDG_CONFIG_HOME/pharo-mcp/tools.json` when `XDG_CONFIG_HOME` is an absolute +path, else `%APPDATA%\pharo-mcp\tools.json` on Windows and +`~/.config/pharo-mcp/tools.json` elsewhere, the folder of +[`otel.env`](observability.md). + +```json +{ + "advertise": ["test_coverage_run"], + "unadvertise": ["critique_run"] +} +``` + +A server reads it when it starts, and it changes the defaults only: a server +configured as below keeps the tools its code chose. A name no tool in the +image has changes nothing, since the tool may come from a project the image +does not load, and a name in both lists keeps its default. A file that cannot +be read, or holds no list of names, changes nothing and stops no server. + Against that default, a server is configured directly: ```smalltalk diff --git a/docs/user/getting-started.md b/docs/user/getting-started.md index a3f365b5..1151175c 100644 --- a/docs/user/getting-started.md +++ b/docs/user/getting-started.md @@ -107,6 +107,9 @@ method_get ``` The specialized tools are found with `tool_search`. +Which tools `tools/list` shows can be changed for every image you run, in +`~/.config/pharo-mcp/tools.json`: see +[Shaping the server's surface](building-on-mcp.md#shaping-the-servers-surface). Before mutation, inspect the target and use a copied or disposable image. Prefer dedicated tools such as `class_create`, `method_compile`, refactoring diff --git a/src/MCP-OpenTelemetry/MCPOpenTelemetrySettings.class.st b/src/MCP-OpenTelemetry/MCPOpenTelemetrySettings.class.st index 6555600b..f52fc658 100644 --- a/src/MCP-OpenTelemetry/MCPOpenTelemetrySettings.class.st +++ b/src/MCP-OpenTelemetry/MCPOpenTelemetrySettings.class.st @@ -1,49 +1,20 @@ " -MCP's telemetry settings, as an image reads them: MCP's own settings file, whose settings replace every OTEL_* variable the image inherited, so that whoever wrote it decides where MCP's telemetry goes whatever program started the image; or else, with no file or a file that gives none, the OTEL_* environment. +MCP's telemetry settings, as an image reads them: otel.env in MCP's folder of the user's configuration, whose settings replace every OTEL_* variable the image inherited, so that whoever wrote it decides where MCP's telemetry goes whatever program started the image; or else, with no file or a file that gives none, the OTEL_* environment. Reads its environment through at:ifAbsent: and keys, and writes it through at:put: and removeKey:, so a test gives it a Dictionary. " Class { #name : 'MCPOpenTelemetrySettings', - #superclass : 'Object', - #instVars : [ - 'environment', - 'windows', - 'home' - ], + #superclass : 'MCPUserSettings', #category : 'MCP-OpenTelemetry', #package : 'MCP-OpenTelemetry' } -{ #category : 'instance creation' } -MCPOpenTelemetrySettings class >> current [ - "The settings of this image's process." - - ^ self new - environment: OSEnvironment current; - windows: OSPlatform current isWindows; - home: self homeDirectory; - yourself -] - { #category : 'instance creation' } MCPOpenTelemetrySettings class >> environment: aDictionary [ "Settings read from aDictionary instead of the process's environment, on a Unix-like system." - ^ self new - environment: aDictionary; - windows: false; - home: self homeDirectory; - yourself -] - -{ #category : 'accessing' } -MCPOpenTelemetrySettings class >> homeDirectory [ - "The user's home directory, or nil when the process cannot tell, as when HOME is unset: then there is no settings file to read there, which is no reason to fail." - - ^ [ FileLocator home asFileReference ] - on: Error - do: [ :error | error return: nil ] + ^ self withEnvironment: aDictionary ] { #category : 'applying' } @@ -61,12 +32,6 @@ MCPOpenTelemetrySettings >> apply [ ^ settings ] -{ #category : 'initialization' } -MCPOpenTelemetrySettings >> environment: aDictionary [ - - environment := aDictionary -] - { #category : 'accessing' } MCPOpenTelemetrySettings >> fileSettings [ "The OTEL_* settings the file's KEY=value lines give, as a dotenv file writes them. Blank lines and # comments are skipped, as are a leading export, a # comment after a value, and quotes around a value; any other key is not the file's to set. No file gives none." @@ -88,24 +53,6 @@ MCPOpenTelemetrySettings >> fileSettings [ ^ settings ] -{ #category : 'initialization' } -MCPOpenTelemetrySettings >> home: aFileReference [ - - home := aFileReference -] - -{ #category : 'testing' } -MCPOpenTelemetrySettings >> isAbsolutePath: aString [ - "Whether Node, which writes this file for the tools that do, reads aString as an absolute path on this system: one that starts at the root, and on Windows also one that starts with a separator or a drive letter, a colon and a separator, either way round (C:\ or C:/)." - - aString isEmpty ifTrue: [ ^ false ]. - windows ifFalse: [ ^ aString first = $/ ]. - ('/\' includes: aString first) ifTrue: [ ^ true ]. - ^ aString size >= 3 and: [ - aString first asInteger < 128 and: [ - aString first isLetter and: [ (aString at: 2) = $: and: [ '/\' includes: (aString at: 3) ] ] ] ] -] - { #category : 'testing' } MCPOpenTelemetrySettings >> isConfigured [ "Whether the settings ask for traces to be exported: the SDK is not disabled, and either a configuration file is named or the traces exporter is OTLP, the default, with an endpoint." @@ -137,14 +84,9 @@ MCPOpenTelemetrySettings >> settingNamesInEnvironment [ { #category : 'accessing' } MCPOpenTelemetrySettings >> settingsFile [ - "Where MCP's settings live: $XDG_CONFIG_HOME/pharo-mcp/otel.env when that is an absolute path, %APPDATA%\pharo-mcp\otel.env on Windows, ~/.config/pharo-mcp/otel.env otherwise; nil with no home to find it in." - - | configHome appData | - configHome := self valueAt: 'XDG_CONFIG_HOME'. - (configHome isNotNil and: [ self isAbsolutePath: configHome ]) ifTrue: [ ^ configHome asFileReference / 'pharo-mcp' / 'otel.env' ]. - appData := self valueAt: 'APPDATA'. - (windows and: [ appData isNotNil ]) ifTrue: [ ^ appData asFileReference / 'pharo-mcp' / 'otel.env' ]. - ^ home ifNotNil: [ home / '.config' / 'pharo-mcp' / 'otel.env' ] + "Where MCP's telemetry settings live: otel.env in MCP's folder of the user's configuration; nil with no home to find it in." + + ^ self fileNamed: 'otel.env' ] { #category : 'accessing' } @@ -154,30 +96,6 @@ MCPOpenTelemetrySettings >> settingsInEffect [ ^ (self settingNamesInEnvironment collect: [ :each | each asString -> (self valueAt: each) ]) asDictionary ] -{ #category : 'private' } -MCPOpenTelemetrySettings >> textOf: aFile [ - "The file's text: UTF-8, or else its bytes as Latin-1, so that a byte an editor wrote in another encoding, as in a comment, does not cost the whole file. The keys and the values that count are ASCII either way." - - | bytes | - bytes := aFile binaryReadStreamDo: [ :stream | stream upToEnd ]. - ^ [ ZnCharacterEncoder utf8 decodeBytes: bytes ] - on: Error - do: [ :error | error return: bytes asString ] -] - -{ #category : 'accessing' } -MCPOpenTelemetrySettings >> valueAt: aName [ - "The variable's value, trimmed, or nil when it is unset or blank." - - | value | - value := [ environment at: aName ifAbsent: [ nil ] ] - on: Error - do: [ nil ]. - value ifNil: [ ^ nil ]. - value := value asString trimBoth. - ^ value ifEmpty: [ nil ] -] - { #category : 'private' } MCPOpenTelemetrySettings >> valueIn: aString [ "The value a line gives after its =: what quotes hold, # included, when a quote closes and nothing but a comment follows; otherwise the text before a # that starts it or follows a space or tab." @@ -195,9 +113,3 @@ MCPOpenTelemetrySettings >> valueIn: aString [ ifNone: [ ^ value ]. ^ (value first: comment - 1) trimBoth ] - -{ #category : 'initialization' } -MCPOpenTelemetrySettings >> windows: aBoolean [ - - windows := aBoolean -] diff --git a/src/MCP-Tests/MCPServerLifecycleTest.class.st b/src/MCP-Tests/MCPServerLifecycleTest.class.st index c1a0a8ad..e2b88e95 100644 --- a/src/MCP-Tests/MCPServerLifecycleTest.class.st +++ b/src/MCP-Tests/MCPServerLifecycleTest.class.st @@ -94,6 +94,29 @@ MCPServerLifecycleTest >> testARunningServerTellsTheMachineToReadItsSockets [ self assert: second isTerminated ] +{ #category : 'tests' } +MCPServerLifecycleTest >> testAServerTakesItsUsersChoiceOfToolsWhenItStarts [ + "Whatever started the image, as PLexus does with MCP new, port: 0 and start." + + | directory server | + directory := FileLocator temp asFileReference / ('mcp-tool-choice-' , UUID new asString). + (directory / 'pharo-mcp') ensureCreateDirectory. + directory / 'pharo-mcp' / 'tools.json' writeStreamDo: [ :stream | + stream nextPutAll: '{ "advertise": ["repository_search"], "unadvertise": ["tool_search"] }' ]. + server := nil. + [ + server := MCP new. + server userSettings: (MCPUserSettings withEnvironment: (Dictionary newFrom: { ('XDG_CONFIG_HOME' -> directory fullName) })). + server port: 0. + self assert: (server advertisedToolNames includes: 'tool_search'). + server start. + self assert: (server advertisedToolNames includes: 'repository_search'). + self deny: (server advertisedToolNames includes: 'tool_search'). + self assert: ((server rpcToolsList at: #tools) anySatisfy: [ :each | (each at: #name) = 'repository_search' ]) ] ensure: [ + self stopAndForget: server. + directory ensureDeleteAll ] +] + { #category : 'tests' } MCPServerLifecycleTest >> testAStartedServerIsListedEvenWhenNothingHoldsIt [ "A started server is held by its own process and socket, which is what turns a port held by a forgotten server into something visible." diff --git a/src/MCP-Tests/MCPToolChoiceTest.class.st b/src/MCP-Tests/MCPToolChoiceTest.class.st new file mode 100644 index 00000000..39327986 --- /dev/null +++ b/src/MCP-Tests/MCPToolChoiceTest.class.st @@ -0,0 +1,159 @@ +" +A user's choice of advertised tools, read from tools.json in MCP's folder of their configuration: the tools it moves across a server's defaults, and what a server configured in code keeps. +" +Class { + #name : 'MCPToolChoiceTest', + #superclass : 'TestCase', + #instVars : [ + 'directory' + ], + #category : 'MCP-Tests', + #package : 'MCP-Tests' +} + +{ #category : 'running' } +MCPToolChoiceTest >> setUp [ + + super setUp. + directory := FileLocator temp asFileReference / ('mcp-tool-choice-' , UUID new asString). + directory ensureCreateDirectory +] + +{ #category : 'private' } +MCPToolChoiceTest >> settingsWithFile: aString [ + "User settings whose configuration folder is this test's directory, its tools.json holding aString, or no file when aString is nil." + + aString ifNotNil: [ + (directory / 'pharo-mcp') ensureCreateDirectory. + directory / 'pharo-mcp' / 'tools.json' writeStreamDo: [ :stream | stream nextPutAll: aString ] ]. + ^ MCPUserSettings withEnvironment: (Dictionary newFrom: { ('XDG_CONFIG_HOME' -> directory fullName) }) +] + +{ #category : 'running' } +MCPToolChoiceTest >> tearDown [ + + directory ensureDeleteAll. + super tearDown +] + +{ #category : 'tests' } +MCPToolChoiceTest >> testAChoiceMovesToolsAcrossTheDefaults [ + + | registry | + registry := MCPToolRegistry default. + registry toolChoice: (MCPToolChoice advertise: #( 'repository_search' ) unadvertise: #( 'tool_search' )). + self assert: (registry isToolAdvertisedNamed: 'repository_search'). + self deny: (registry isToolAdvertisedNamed: 'tool_search'). + self assert: (registry isToolAdvertisedNamed: 'class_get'). + self deny: (registry isToolAdvertisedNamed: 'method_update') +] + +{ #category : 'tests' } +MCPToolChoiceTest >> testANameInBothListsKeepsItsDefault [ + + | choice | + choice := MCPToolChoice advertise: #( 'tool_search' 'repository_search' ) unadvertise: #( 'tool_search' 'repository_search' ). + self assert: (choice advertisesToolNamed: 'tool_search' byDefault: true). + self deny: (choice advertisesToolNamed: 'repository_search' byDefault: false) +] + +{ #category : 'tests' } +MCPToolChoiceTest >> testANameNoToolHasIsSkipped [ + "The tool may come from a project this image does not load, or from another release." + + | registry | + registry := MCPToolRegistry default. + registry toolChoice: (MCPToolChoice advertise: #( 'no_such_tool' 'repository_search' ) unadvertise: #( 'another_missing_tool' )). + self assert: (registry isToolAdvertisedNamed: 'repository_search'). + self deny: (registry advertisedToolNames includes: 'no_such_tool') +] + +{ #category : 'tests' } +MCPToolChoiceTest >> testAServerConfiguredInCodeKeepsItsTools [ + "The choice changes the defaults, so a server whose code chose its tools keeps them." + + | registry | + registry := MCPToolRegistry default. + registry toolChoice: (MCPToolChoice advertise: #( 'repository_search' ) unadvertise: #( 'class_get' )). + registry advertiseTools: #( 'class_get' ). + self assert: registry advertisedToolNames equals: #( 'class_get' ). + registry advertiseAllTools. + self assert: (registry isToolAdvertisedNamed: 'class_get') +] + +{ #category : 'tests' } +MCPToolChoiceTest >> testAToolAdvertisedInCodeKeepsTheChoice [ + "advertiseTool: adds one tool to what is advertised, the user's choice included." + + | registry | + registry := MCPToolRegistry default. + registry toolChoice: (MCPToolChoice advertise: #( 'repository_search' ) unadvertise: #( 'tool_search' )). + registry advertiseTool: 'method_update'. + self assert: (registry isToolAdvertisedNamed: 'repository_search'). + self deny: (registry isToolAdvertisedNamed: 'tool_search'). + self assert: (registry isToolAdvertisedNamed: 'method_update') +] + +{ #category : 'tests' } +MCPToolChoiceTest >> testTheFileGivesTheChoice [ + + | choice | + choice := MCPToolChoice of: (self settingsWithFile: '{ "advertise": ["test_coverage_run"], "unadvertise": ["critique_run"] }'). + self assert: (choice advertisesToolNamed: 'test_coverage_run' byDefault: false). + self deny: (choice advertisesToolNamed: 'critique_run' byDefault: true). + self assert: (choice advertisesToolNamed: 'class_get' byDefault: true) +] + +{ #category : 'tests' } +MCPToolChoiceTest >> testTheFileIsReadLeniently [ + "A single name for a list, blanks around a name, entries that are not names, and keys it does not know." + + | choice | + choice := MCPToolChoice of: + (self settingsWithFile: + '{ "advertise": " test_coverage_run ", "unadvertise": ["critique_run", 3, null, ""], "comment": "mine" }'). + self assert: (choice advertisesToolNamed: 'test_coverage_run' byDefault: false). + self deny: (choice advertisesToolNamed: 'critique_run' byDefault: true). + self assert: (choice advertisesToolNamed: 'class_get' byDefault: true) +] + +{ #category : 'tests' } +MCPToolChoiceTest >> testTheFilesAreInMCPsConfigurationFolder [ + + | home | + home := FileLocator home asFileReference. + self + assert: ((MCPUserSettings withEnvironment: (Dictionary newFrom: { ('XDG_CONFIG_HOME' -> '/config') })) fileNamed: 'tools.json') + equals: '/config/pharo-mcp/tools.json' asFileReference. + self + assert: ((MCPUserSettings withEnvironment: (Dictionary newFrom: { ('XDG_CONFIG_HOME' -> 'relative') })) fileNamed: 'tools.json') + equals: home / '.config' / 'pharo-mcp' / 'tools.json'. + self + assert: (((MCPUserSettings withEnvironment: (Dictionary newFrom: { ('APPDATA' -> '/roaming') })) + windows: true; + yourself) fileNamed: 'tools.json') + equals: '/roaming/pharo-mcp/tools.json' asFileReference +] + +{ #category : 'tests' } +MCPToolChoiceTest >> testWithoutAHomeNothingIsRead [ + "HOME may be unset, as in a service or a container: then there is no file to read." + + | settings | + settings := (MCPUserSettings withEnvironment: Dictionary new) + home: nil; + yourself. + self assert: (settings fileNamed: 'tools.json') isNil. + self assert: ((MCPToolChoice of: settings) advertisesToolNamed: 'class_get' byDefault: true) +] + +{ #category : 'tests' } +MCPToolChoiceTest >> testWithoutAUsableFileNothingChanges [ + "No file, an empty one, one that is not JSON, and JSON that holds no lists of names." + + #( nil '' 'not json' '[ "class_get" ]' '{ "advertise": { "repository_search": true } }' ) do: [ :text | + | choice | + choice := MCPToolChoice of: (self settingsWithFile: text). + self assert: (choice advertisesToolNamed: 'class_get' byDefault: true) description: text printString. + self deny: (choice advertisesToolNamed: 'repository_search' byDefault: false) description: text printString ] +] diff --git a/src/MCP/MCP.class.st b/src/MCP/MCP.class.st index 514572c4..bc1d8c0b 100644 --- a/src/MCP/MCP.class.st +++ b/src/MCP/MCP.class.st @@ -10,6 +10,7 @@ Two words govern the tool surface. A *registered* tool is in the catalog, callab - `mcp onlyTools: { A. B }` replaces the catalog with exactly those; - `mcp advertiseTools: { A. 'class_search' }` chooses what tools/list shows, registering anything not yet in the catalog; - `mcp advertiseAllTools` and `mcp advertiseToolDefaults` are the other two advertising states; +- the defaults are each tool's own default exposure, as the user's tools.json moves them, which `start` reads (MCPToolChoice); - a tool class is in the catalog because its package was loaded, wherever it was defined; `MyToolClass class >> shouldAutoRegister` answering `false` keeps one out. " Class { @@ -24,7 +25,8 @@ Class { 'promptRegistry', 'toolResponseFields', 'lastClientInfo', - 'registryMutex' + 'registryMutex', + 'userSettings' ], #classVars : [ 'DefaultObservabilityBackendClass' @@ -238,6 +240,13 @@ MCP >> addTools: toolsOrToolClasses [ ^ added ] +{ #category : 'configuration' } +MCP >> adoptUsersToolChoice [ + "Take the user's choice of advertised tools from their tools.json. It moves tools across the defaults only, so tools this server's code chose stay as they are." + + self toolRegistry toolChoice: (MCPToolChoice of: self userSettings) +] + { #category : 'configuration' } MCP >> advertiseAllTools [ @@ -1028,6 +1037,7 @@ MCP >> serverInfrastructureMetadata [ { #category : 'start' } MCP >> start [ + self adoptUsersToolChoice. self refreshToolsList. server addHandlersFromPragmasIn: self. server start. @@ -1124,6 +1134,19 @@ MCP >> useStructuredContentOnlyToolResponses [ self toolResponseFields: #structuredContent ] +{ #category : 'configuration' } +MCP >> userSettings [ + "The files the user keeps for every MCP server they run." + + ^ userSettings ifNil: [ userSettings := MCPUserSettings current ] +] + +{ #category : 'configuration' } +MCP >> userSettings: someUserSettings [ + + userSettings := someUserSettings +] + { #category : 'configuration' } MCP >> validToolResponseFields [ diff --git a/src/MCP/MCPToolChoice.class.st b/src/MCP/MCPToolChoice.class.st new file mode 100644 index 00000000..cc744066 --- /dev/null +++ b/src/MCP/MCPToolChoice.class.st @@ -0,0 +1,77 @@ +" +A user's choice of advertised tools, which moves tools across MCP's defaults: those it advertises, which servers show in tools/list though their default is to be discovered, and those it unadvertises, which servers leave to discovery though their default is to be listed. It changes the defaults only, so a server whose code chose its tools keeps them. + +Read from tools.json in MCP's folder of the user's configuration (MCPUserSettings), as +{ ""advertise"": [ ""test_coverage_run"" ], ""unadvertise"": [ ""critique_run"" ] } +A name no tool has changes nothing, and a name in both lists keeps its default. +" +Class { + #name : 'MCPToolChoice', + #superclass : 'Object', + #instVars : [ + 'advertised', + 'unadvertised' + ], + #category : 'MCP-Registries', + #package : 'MCP', + #tag : 'Registries' +} + +{ #category : 'instance creation' } +MCPToolChoice class >> advertise: someNames unadvertise: otherNames [ + + ^ self new setAdvertised: someNames unadvertised: otherNames +] + +{ #category : 'private' } +MCPToolChoice class >> namesIn: aValue [ + "The tool names a list gives, or the one a lone name is; anything else gives none." + + | values | + values := aValue isString + ifTrue: [ { aValue } ] + ifFalse: [ + (aValue isKindOf: SequenceableCollection) + ifTrue: [ aValue ] + ifFalse: [ #( ) ] ]. + ^ (values select: [ :each | each isString ] thenCollect: [ :each | each trimBoth ]) reject: [ :each | each isEmpty ] +] + +{ #category : 'instance creation' } +MCPToolChoice class >> none [ + "The choice of a user who made none: every tool keeps its default." + + ^ self advertise: #( ) unadvertise: #( ) +] + +{ #category : 'instance creation' } +MCPToolChoice class >> of: someUserSettings [ + "The choice the user's tools.json gives, or none when there is no such file or it gives no list of names: a user's file never stops a server." + + | json | + json := [ STONJSON fromString: ((someUserSettings textOfFileNamed: 'tools.json') ifNil: [ ^ self none ]) ] + on: Error + do: [ :error | ^ self none ]. + json isDictionary ifFalse: [ ^ self none ]. + ^ self + advertise: (self namesIn: (json at: 'advertise' ifAbsent: [ nil ])) + unadvertise: (self namesIn: (json at: 'unadvertise' ifAbsent: [ nil ])) +] + +{ #category : 'testing' } +MCPToolChoice >> advertisesToolNamed: aName byDefault: aBoolean [ + "Whether a server that leaves advertising to the defaults lists this tool, whose default is aBoolean. A name in both lists keeps its default." + + | up down | + up := advertised includes: aName asString. + down := unadvertised includes: aName asString. + up = down ifTrue: [ ^ aBoolean ]. + ^ up +] + +{ #category : 'initialization' } +MCPToolChoice >> setAdvertised: someNames unadvertised: otherNames [ + + advertised := someNames collect: [ :each | each asString ] as: Set. + unadvertised := otherNames collect: [ :each | each asString ] as: Set +] diff --git a/src/MCP/MCPToolRegistry.class.st b/src/MCP/MCPToolRegistry.class.st index 305f0b82..a2466a9d 100644 --- a/src/MCP/MCPToolRegistry.class.st +++ b/src/MCP/MCPToolRegistry.class.st @@ -1,7 +1,7 @@ " Instance-scoped catalog of MCP tools. -Two words, consistently. A *registered* tool is in the catalog and reachable through tool_search, tool_get and tool_call. An *advertised* tool is also in tools/list. advertisedToolNames carries that choice in three states: nil leaves it to each tool's own default exposure, #all advertises everything, and a set of names advertises exactly those. +Two words, consistently. A *registered* tool is in the catalog and reachable through tool_search, tool_get and tool_call. An *advertised* tool is also in tools/list. advertisedToolNames carries that choice in three states: nil leaves it to the defaults, #all advertises everything, and a set of names advertises exactly those. The defaults are each tool's own default exposure, as the user's toolChoice moves them. Holding, discovery and rediscovery come from MCPRegistry. What lives here is the advertising split, which the other registries have no equivalent of, and the ranking tool_search uses. " @@ -9,7 +9,8 @@ Class { #name : 'MCPToolRegistry', #superclass : 'MCPRegistry', #instVars : [ - 'advertisedToolNames' + 'advertisedToolNames', + 'toolChoice' ], #category : 'MCP-Registries', #package : 'MCP', @@ -189,7 +190,7 @@ MCPToolRegistry >> isTool: aTool rankedBefore: anotherTool [ { #category : 'advertising' } MCPToolRegistry >> isToolAdvertised: aTool [ - advertisedToolNames ifNil: [ ^ aTool defaultExposure = 'static' ]. + advertisedToolNames ifNil: [ ^ self toolChoice advertisesToolNamed: aTool name byDefault: aTool defaultExposure = 'static' ]. advertisedToolNames = #all ifTrue: [ ^ true ]. ^ advertisedToolNames includes: aTool name ] @@ -325,6 +326,20 @@ MCPToolRegistry >> singularSearchTermFor: aTerm [ ^ term ] +{ #category : 'advertising' } +MCPToolRegistry >> toolChoice [ + "The user's choice of advertised tools, which moves tools across their default exposure while advertising is left to the defaults." + + ^ toolChoice ifNil: [ MCPToolChoice none ] +] + +{ #category : 'advertising' } +MCPToolRegistry >> toolChoice: aToolChoice [ + + toolChoice := aToolChoice. + self noteChanged +] + { #category : 'tools' } MCPToolRegistry >> toolGroups [ diff --git a/src/MCP/MCPUserSettings.class.st b/src/MCP/MCPUserSettings.class.st new file mode 100644 index 00000000..82977eb0 --- /dev/null +++ b/src/MCP/MCPUserSettings.class.st @@ -0,0 +1,132 @@ +" +The files a user keeps for every MCP server they run, in MCP's folder of their configuration: $XDG_CONFIG_HOME/pharo-mcp when that is an absolute path, %APPDATA%\pharo-mcp on Windows, ~/.config/pharo-mcp otherwise. + +Reads its environment through at:ifAbsent:, so a test gives it a Dictionary. +" +Class { + #name : 'MCPUserSettings', + #superclass : 'Object', + #instVars : [ + 'environment', + 'windows', + 'home' + ], + #category : 'MCP-Server', + #package : 'MCP', + #tag : 'Server' +} + +{ #category : 'instance creation' } +MCPUserSettings class >> current [ + "The settings of this image's process." + + ^ self new + environment: OSEnvironment current; + windows: OSPlatform current isWindows; + home: self homeDirectory; + yourself +] + +{ #category : 'accessing' } +MCPUserSettings class >> homeDirectory [ + "The user's home directory, or nil when the process cannot tell, as when HOME is unset: then there is no settings file to read there, which is no reason to fail." + + ^ [ FileLocator home asFileReference ] + on: Error + do: [ :error | error return: nil ] +] + +{ #category : 'instance creation' } +MCPUserSettings class >> withEnvironment: aDictionary [ + "Settings read from aDictionary instead of the process's environment, on a Unix-like system." + + ^ self new + environment: aDictionary; + windows: false; + home: self homeDirectory; + yourself +] + +{ #category : 'initialization' } +MCPUserSettings >> environment: aDictionary [ + + environment := aDictionary +] + +{ #category : 'accessing' } +MCPUserSettings >> fileNamed: aString [ + "The file of that name in MCP's folder of the user's configuration, or nil with no folder to find it in." + + ^ self folder ifNotNil: [ :folder | folder / aString ] +] + +{ #category : 'accessing' } +MCPUserSettings >> folder [ + "MCP's folder of the user's configuration: $XDG_CONFIG_HOME/pharo-mcp when that is an absolute path, %APPDATA%\pharo-mcp on Windows, ~/.config/pharo-mcp otherwise; nil with no home to find it in." + + | configHome appData | + configHome := self valueAt: 'XDG_CONFIG_HOME'. + (configHome isNotNil and: [ self isAbsolutePath: configHome ]) ifTrue: [ ^ configHome asFileReference / 'pharo-mcp' ]. + appData := self valueAt: 'APPDATA'. + (windows and: [ appData isNotNil ]) ifTrue: [ ^ appData asFileReference / 'pharo-mcp' ]. + ^ home ifNotNil: [ home / '.config' / 'pharo-mcp' ] +] + +{ #category : 'initialization' } +MCPUserSettings >> home: aFileReference [ + + home := aFileReference +] + +{ #category : 'testing' } +MCPUserSettings >> isAbsolutePath: aString [ + "Whether Node, which writes these files for the tools that do, reads aString as an absolute path on this system: one that starts at the root, and on Windows also one that starts with a separator or a drive letter, a colon and a separator, either way round (C:\ or C:/)." + + aString isEmpty ifTrue: [ ^ false ]. + windows ifFalse: [ ^ aString first = $/ ]. + ('/\' includes: aString first) ifTrue: [ ^ true ]. + ^ aString size >= 3 and: [ + aString first asInteger < 128 and: [ + aString first isLetter and: [ (aString at: 2) = $: and: [ '/\' includes: (aString at: 3) ] ] ] ] +] + +{ #category : 'private' } +MCPUserSettings >> textOf: aFile [ + "The file's text: UTF-8, or else its bytes as Latin-1, so that a byte an editor wrote in another encoding, as in a comment, does not cost the whole file." + + | bytes | + bytes := aFile binaryReadStreamDo: [ :stream | stream upToEnd ]. + ^ [ ZnCharacterEncoder utf8 decodeBytes: bytes ] + on: Error + do: [ :error | error return: bytes asString ] +] + +{ #category : 'accessing' } +MCPUserSettings >> textOfFileNamed: aString [ + "The text of the file of that name, or nil when there is none or it cannot be read: a user's file never stops a server." + + | file | + file := (self fileNamed: aString) ifNil: [ ^ nil ]. + ^ [ self textOf: file ] + on: Error + do: [ :error | error return: nil ] +] + +{ #category : 'accessing' } +MCPUserSettings >> valueAt: aName [ + "The variable's value, trimmed, or nil when it is unset or blank." + + | value | + value := [ environment at: aName ifAbsent: [ nil ] ] + on: Error + do: [ nil ]. + value ifNil: [ ^ nil ]. + value := value asString trimBoth. + ^ value ifEmpty: [ nil ] +] + +{ #category : 'initialization' } +MCPUserSettings >> windows: aBoolean [ + + windows := aBoolean +]