-
Notifications
You must be signed in to change notification settings - Fork 5
feat(setup): explicit context API for multi-instance ServerContext embedding #150
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
Merged
wpak-ai
merged 5 commits into
cppalliance:main
from
jonathanMLDev:feat/server-context-setup-api
Jun 11, 2026
Merged
Changes from all commits
Commits
Show all changes
5 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,93 @@ | ||
| import { ALLIANCE_SERVER_INSTRUCTIONS } from '../constants.js'; | ||
| import type { ServerConfig } from '../core/config.js'; | ||
| import { getDefaultServerContext } from '../core/server/server-context.js'; | ||
| import { getDefaultServerContext, type ServerContext } from '../core/server/server-context.js'; | ||
| import { resolveAllianceConfig } from './config.js'; | ||
| import { setupCoreServer, type ServerHandle } from '../core/setup.js'; | ||
| import { setupCoreServer, type ServerHandle, type SetupCoreServerOptions } from '../core/setup.js'; | ||
| import { registerBuiltinUrlGenerators } from './url-builtins.js'; | ||
| import { registerGuidedQueryTool } from './tools/guided-query-tool.js'; | ||
| import { registerSuggestQueryParamsTool } from './tools/suggest-query-params-tool.js'; | ||
|
|
||
| /** | ||
| * Options for {@link setupAllianceServer}. | ||
| */ | ||
| export type SetupAllianceServerOptions = { | ||
| config?: ServerConfig; | ||
| context?: ServerContext; | ||
| /** MCP server instructions; defaults to {@link ALLIANCE_SERVER_INSTRUCTIONS}. */ | ||
| instructions?: string; | ||
| }; | ||
|
|
||
| function isServerConfig(value: unknown): value is ServerConfig { | ||
| return ( | ||
| typeof value === 'object' && | ||
| value !== null && | ||
| typeof (value as ServerConfig).apiKey === 'string' && | ||
| typeof (value as ServerConfig).indexName === 'string' | ||
| ); | ||
| } | ||
|
|
||
| function isSetupAllianceServerOptions(value: unknown): value is SetupAllianceServerOptions { | ||
| if (typeof value !== 'object' || value === null || isServerConfig(value)) { | ||
| return false; | ||
| } | ||
| for (const key of Object.keys(value as Record<string, unknown>)) { | ||
| if (key !== 'config' && key !== 'context' && key !== 'instructions') { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| function normalizeSetupAllianceArgs( | ||
| configOrOptions?: ServerConfig | SetupAllianceServerOptions, | ||
| legacyOptions?: Pick<SetupAllianceServerOptions, 'instructions'> | ||
| ): SetupAllianceServerOptions { | ||
| if (configOrOptions === undefined) { | ||
| return legacyOptions ?? {}; | ||
| } | ||
| if (isServerConfig(configOrOptions)) { | ||
| return { config: configOrOptions, ...legacyOptions }; | ||
| } | ||
| if (isSetupAllianceServerOptions(configOrOptions)) { | ||
| return { ...configOrOptions, ...legacyOptions }; | ||
| } | ||
| throw new TypeError('configOrOptions must be a ServerConfig or SetupAllianceServerOptions'); | ||
| } | ||
|
|
||
| /** | ||
| * Create and configure the MCP server with the full Alliance tool surface: | ||
| * all core tools plus `suggest_query_params`, `guided_query`, and built-in URL generators. | ||
| * | ||
| * When `config` is omitted, resolves env via {@link resolveAllianceConfig} (Alliance index/rerank defaults when unset). | ||
| */ | ||
| export async function setupAllianceServer(config?: ServerConfig): Promise<ServerHandle> { | ||
| const server = await setupCoreServer(config ?? resolveAllianceConfig({}), { | ||
| instructions: ALLIANCE_SERVER_INSTRUCTIONS, | ||
| }); | ||
| const ctx = getDefaultServerContext(); | ||
| registerBuiltinUrlGenerators(ctx); | ||
| registerSuggestQueryParamsTool(server, ctx); | ||
| registerGuidedQueryTool(server, ctx); | ||
| export async function setupAllianceServer( | ||
| configOrOptions?: ServerConfig | SetupAllianceServerOptions, | ||
| legacyOptions?: Pick<SetupAllianceServerOptions, 'instructions'> | ||
| ): Promise<ServerHandle> { | ||
| const opts = normalizeSetupAllianceArgs(configOrOptions, legacyOptions); | ||
| const instructions = opts.instructions ?? ALLIANCE_SERVER_INSTRUCTIONS; | ||
|
|
||
| let server: ServerHandle; | ||
| let resolvedCtx: ServerContext; | ||
|
|
||
| if (opts.context) { | ||
| resolvedCtx = opts.context; | ||
| const coreOpts: SetupCoreServerOptions = { context: resolvedCtx, instructions }; | ||
| if (opts.config !== undefined) { | ||
| coreOpts.config = opts.config; | ||
| } | ||
| server = await setupCoreServer(coreOpts); | ||
| } else { | ||
| const config = opts.config ?? resolveAllianceConfig({}); | ||
| server = await setupCoreServer({ | ||
| config: opts.config ?? config, | ||
| instructions, | ||
| }); | ||
| resolvedCtx = getDefaultServerContext(); | ||
| } | ||
|
|
||
| registerBuiltinUrlGenerators(resolvedCtx); | ||
| registerSuggestQueryParamsTool(server, resolvedCtx); | ||
| registerGuidedQueryTool(server, resolvedCtx); | ||
| return server; | ||
| } |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.