-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add OpenCode MCP client configuration #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
travis-hoover-glean
wants to merge
2
commits into
main
Choose a base branch
from
thoov/add-opencode-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| { | ||
| "id": "opencode", | ||
| "name": "opencode", | ||
| "displayName": "OpenCode", | ||
| "description": "OpenCode with native HTTP and stdio support", | ||
| "userConfigurable": true, | ||
| "documentationUrl": "https://opencode.ai/docs/mcp-servers/", | ||
| "transports": ["stdio", "http"], | ||
| "supportedPlatforms": ["darwin", "linux", "win32"], | ||
| "configFormat": "json", | ||
| "configPath": { | ||
| "darwin": "$HOME/.config/opencode/opencode.json", | ||
| "linux": "$HOME/.config/opencode/opencode.json", | ||
| "win32": "%USERPROFILE%\\.config\\opencode\\opencode.json" | ||
| }, | ||
| "configStructure": { | ||
| "serversPropertyName": "mcp", | ||
| "httpPropertyMapping": { | ||
| "typeProperty": "type", | ||
| "urlProperty": "url", | ||
| "headersProperty": "headers" | ||
| }, | ||
| "stdioPropertyMapping": { | ||
| "typeProperty": "type", | ||
| "commandProperty": "command", | ||
| "argsProperty": "command", | ||
| "envProperty": "environment" | ||
| } | ||
| }, | ||
| "supportedAuth": ["token", "oauth:dcr"], | ||
| "oauth": { | ||
| "dcr": { | ||
| "redirect_uri_patterns": ["http://127.0.0.1:*"] | ||
| } | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
packages/mcp-config-schema/examples/configs/http/opencode.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "mcp": { | ||
| "example": { | ||
| "type": "remote", | ||
| "url": "https://api.example.com/mcp" | ||
| } | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
packages/mcp-config-schema/examples/configs/stdio/opencode.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "mcp": { | ||
| "example": { | ||
| "type": "local", | ||
| "command": [ | ||
| "npx", | ||
| "-y", | ||
| "@example/mcp-server" | ||
| ], | ||
| "environment": { | ||
| "EXAMPLE_API_KEY": "your-api-key" | ||
| } | ||
| } | ||
| } | ||
| } |
133 changes: 133 additions & 0 deletions
133
packages/mcp-config-schema/src/builders/OpenCodeConfigBuilder.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { BaseConfigBuilder } from './BaseConfigBuilder.js'; | ||
| import { MCPConnectionOptions, OpenCodeMCPConfig, MCPServersRecord } from '../types.js'; | ||
|
|
||
| function isOpenCodeMCPConfig( | ||
| config: OpenCodeMCPConfig | MCPServersRecord | ||
| ): config is OpenCodeMCPConfig { | ||
| return typeof config === 'object' && config !== null && 'mcp' in config; | ||
| } | ||
|
|
||
| /** | ||
| * Config builder for OpenCode which uses { mcp: {...} } format | ||
| * with "local"/"remote" type values and combined command arrays. | ||
| */ | ||
| export class OpenCodeConfigBuilder extends BaseConfigBuilder<OpenCodeMCPConfig> { | ||
| protected buildStdioConfig( | ||
| options: MCPConnectionOptions, | ||
| includeRootObject: boolean = true | ||
| ): Record<string, unknown> { | ||
| const serverName = this.buildServerName({ | ||
| transport: 'stdio', | ||
| serverName: options.serverName, | ||
| }); | ||
|
|
||
| const serverConfig: Record<string, unknown> = { | ||
| type: 'local', | ||
| command: ['npx', '-y', this.serverPackage], | ||
| }; | ||
|
|
||
| const env = this.getEnvVars(options); | ||
| if (env) { | ||
| serverConfig.environment = env; | ||
| } | ||
|
|
||
| if (!includeRootObject) { | ||
| return { | ||
| [serverName]: serverConfig, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| mcp: { | ||
| [serverName]: serverConfig, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| protected buildHttpConfig( | ||
| options: MCPConnectionOptions, | ||
| includeRootObject: boolean = true | ||
| ): Record<string, unknown> { | ||
| if (!options.serverUrl) { | ||
| throw new Error('HTTP transport requires a server URL'); | ||
| } | ||
|
|
||
| const resolvedUrl = this.substituteUrlVariables(options.serverUrl, options.urlVariables); | ||
|
|
||
| const serverName = this.buildServerName({ | ||
| transport: 'http', | ||
| serverUrl: options.serverUrl, | ||
| serverName: options.serverName, | ||
| }); | ||
|
|
||
| if (this.config.transports.includes('http')) { | ||
| const serverConfig: Record<string, unknown> = { | ||
| type: 'remote', | ||
| url: resolvedUrl, | ||
| }; | ||
|
|
||
| const headers = this.buildHeaders(options); | ||
| if (headers) { | ||
| serverConfig.headers = headers; | ||
| } | ||
|
|
||
| if (!includeRootObject) { | ||
| return { | ||
| [serverName]: serverConfig, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| mcp: { | ||
| [serverName]: serverConfig, | ||
| }, | ||
| }; | ||
| } else { | ||
| throw new Error(`Client ${this.config.id} doesn't support HTTP server configuration`); | ||
| } | ||
| } | ||
|
|
||
| protected buildHttpCommand(options: MCPConnectionOptions): string | null { | ||
| if (this.commandBuilder?.http) { | ||
| return this.commandBuilder.http(this.config.id, options); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| protected buildStdioCommand(options: MCPConnectionOptions): string | null { | ||
| if (this.commandBuilder?.stdio) { | ||
| return this.commandBuilder.stdio(this.config.id, options); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * @deprecated This method is deprecated and will be removed in the next major version. | ||
| * Consumers should use buildConfiguration() directly and handle the output format | ||
| * based on the includeRootObject option. | ||
| */ | ||
| getNormalizedServersConfig(config: OpenCodeMCPConfig | MCPServersRecord): MCPServersRecord { | ||
| const servers: MCPServersRecord = isOpenCodeMCPConfig(config) ? config.mcp : config; | ||
|
|
||
| const normalized: MCPServersRecord = {}; | ||
|
|
||
| for (const [name, value] of Object.entries(servers)) { | ||
| const openCodeConfig = value as Record<string, unknown>; | ||
| if (typeof openCodeConfig === 'object' && openCodeConfig !== null) { | ||
| const commandArray = openCodeConfig.command as string[] | undefined; | ||
| normalized[name] = { | ||
| type: openCodeConfig.type === 'local' ? 'stdio' : 'http', | ||
| command: commandArray?.[0], | ||
| args: commandArray?.slice(1), | ||
| env: openCodeConfig.environment, | ||
| url: openCodeConfig.url, | ||
| headers: openCodeConfig.headers, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return normalized; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getNormalizedServersConfigaccepts either a wrapped{ mcp: ... }object or a flatMCPServersRecord, but this guard only checks whether the keymcpexists. A flat servers map can validly include a server namedmcp(for example whenincludeRootObject: falseandserverNameis"mcp"), in which case the function misclassifies the input as wrapped and then normalizes server fields (type,command, etc.) as if they were server entries, producing corrupted normalized output.Useful? React with 👍 / 👎.