Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/user/building-on-mcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docs/user/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
100 changes: 6 additions & 94 deletions src/MCP-OpenTelemetry/MCPOpenTelemetrySettings.class.st
Original file line number Diff line number Diff line change
@@ -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' }
Expand All @@ -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."
Expand All @@ -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."
Expand Down Expand Up @@ -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' }
Expand All @@ -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."
Expand All @@ -195,9 +113,3 @@ MCPOpenTelemetrySettings >> valueIn: aString [
ifNone: [ ^ value ].
^ (value first: comment - 1) trimBoth
]

{ #category : 'initialization' }
MCPOpenTelemetrySettings >> windows: aBoolean [

windows := aBoolean
]
23 changes: 23 additions & 0 deletions src/MCP-Tests/MCPServerLifecycleTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
159 changes: 159 additions & 0 deletions src/MCP-Tests/MCPToolChoiceTest.class.st
Original file line number Diff line number Diff line change
@@ -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 ]
]
Loading