adds optional stateless configuration (#222)
* optional statelessness * Update config.ts * Update docker-compose.yml * Update documentation --------- Co-authored-by: Harold Spencer Jr. <hspencer@brave.com>
This commit is contained in:
1 parent
1971bc05d6
commit
dd16a2e084
5 files changed
+50
-13
No files matched your search
+23
-3
@@ -34,6 +34,11 @@ export const configSchema = z.object({
|
||||
.default('info')
|
||||
.describe('Desired logging level')
|
||||
.optional(),
|
||||
stateless: z
|
||||
.boolean()
|
||||
.default(false)
|
||||
.describe('Whether the server should be stateless')
|
||||
.optional(),
|
||||
});
|
||||
|
||||
export type SmitheryConfig = z.infer<typeof configSchema>;
|
||||
@@ -46,6 +51,7 @@ type Configuration = {
|
||||
loggingLevel: LoggingLevel;
|
||||
enabledTools: string[];
|
||||
disabledTools: string[];
|
||||
stateless: boolean;
|
||||
};
|
||||
|
||||
const state: Configuration & { ready: boolean } = {
|
||||
@@ -57,6 +63,7 @@ const state: Configuration & { ready: boolean } = {
|
||||
ready: false,
|
||||
enabledTools: [],
|
||||
disabledTools: [],
|
||||
stateless: false,
|
||||
};
|
||||
|
||||
export function isToolPermittedByUser(toolName: string): boolean {
|
||||
@@ -77,12 +84,12 @@ export function getOptions(): Configuration | false {
|
||||
.option(
|
||||
'--enabled-tools <names...>',
|
||||
'tools to enable',
|
||||
process.env.BRAVE_MCP_ENABLED_TOOLS?.split(' ') ?? []
|
||||
process.env.BRAVE_MCP_ENABLED_TOOLS?.trim().split(' ') ?? []
|
||||
)
|
||||
.option(
|
||||
'--disabled-tools <names...>',
|
||||
'tools to disable',
|
||||
process.env.BRAVE_MCP_DISABLED_TOOLS?.split(' ') ?? []
|
||||
process.env.BRAVE_MCP_DISABLED_TOOLS?.trim().split(' ') ?? []
|
||||
)
|
||||
.option(
|
||||
'--port <number>',
|
||||
@@ -94,6 +101,11 @@ export function getOptions(): Configuration | false {
|
||||
'desired host for HTTP transport',
|
||||
process.env.BRAVE_MCP_HOST ?? '0.0.0.0'
|
||||
)
|
||||
.option(
|
||||
'--stateless <boolean>',
|
||||
'whether the server should be stateless',
|
||||
process.env.BRAVE_MCP_STATELESS === 'true' ? true : false
|
||||
)
|
||||
.allowUnknownOption()
|
||||
.parse(process.argv);
|
||||
|
||||
@@ -108,7 +120,11 @@ export function getOptions(): Configuration | false {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ([...enabledTools, ...disabledTools].some((t) => !toolNames.includes(t))) {
|
||||
if (
|
||||
[...enabledTools, ...disabledTools].some(
|
||||
(t) => t.trim().length > 0 && !toolNames.includes(t.trim())
|
||||
)
|
||||
) {
|
||||
console.error(`Invalid tool name used. Must be one of: ${toolNames.join(', ')}`);
|
||||
return false;
|
||||
}
|
||||
@@ -149,6 +165,9 @@ export function getOptions(): Configuration | false {
|
||||
}
|
||||
}
|
||||
|
||||
// Normalize stateless to boolean (CLI passes it as string)
|
||||
options.stateless = options.stateless === true || options.stateless === 'true';
|
||||
|
||||
// Update state
|
||||
state.braveApiKey = options.braveApiKey;
|
||||
state.transport = options.transport;
|
||||
@@ -157,6 +176,7 @@ export function getOptions(): Configuration | false {
|
||||
state.loggingLevel = options.loggingLevel;
|
||||
state.enabledTools = options.enabledTools;
|
||||
state.disabledTools = options.disabledTools;
|
||||
state.stateless = options.stateless;
|
||||
state.ready = true;
|
||||
|
||||
return options as Configuration;
|
||||
|
||||
+16
-7
@@ -37,13 +37,22 @@ const getTransport = async (request: Request): Promise<StreamableHTTPServerTrans
|
||||
return transport;
|
||||
}
|
||||
|
||||
// Otherwise, start a new transport/session
|
||||
const transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => randomUUID(),
|
||||
onsessioninitialized: (sessionId) => {
|
||||
transports.set(sessionId, transport);
|
||||
},
|
||||
});
|
||||
let transport: StreamableHTTPServerTransport;
|
||||
|
||||
if (config.stateless) {
|
||||
// Some contexts (e.g. AgentCore) may prefer or require a stateless transport
|
||||
transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: undefined,
|
||||
});
|
||||
} else {
|
||||
// Otherwise, start a new transport/session
|
||||
transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => randomUUID(),
|
||||
onsessioninitialized: (sessionId) => {
|
||||
transports.set(sessionId, transport);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const mcpServer = createMcpServer();
|
||||
await mcpServer.connect(transport);
|
||||
|
||||
Reference in new issue
Block a user