Repository navigation
Description
When building a multi-tenant application with AgentScope, we need to pass business-specific parameters (like modelConfigId, datasourceId, tenantId) from the Controller layer all the way down to the Middleware/Tool layer via RuntimeContext.
Currently, InboundMessage has no mechanism to carry custom key-value pairs, and ChannelRouter.buildContext only puts agentId into MsgContext.extra. This forces us to use ConcurrentHashMap with userId as the key to simulate context passing:
// Workaround: Store in ConcurrentHashMap before dispatch
modelContext.put(userId, modelConfigId);
datasourceContext.put(userId, datasourceId);
// Workaround: Retrieve in Gateway
private void attachModelContext(RuntimeContext.Builder builder, String userId) {
String modelConfigId = modelContext.resolve(userId);
if (modelConfigId != null) {
builder.put("modelConfigId", modelConfigId);
}
}
// Workaround: Clean up after dispatch
modelContext.remove(userId);
datasourceContext.remove(userId);This approach has several issues:
- Manual lifecycle management - Must remember to
putbefore andremoveafter each call - Memory leak risk - If
removeis missed in error paths, entries accumulate - Thread safety concerns -
putandremoveare not atomic - Unclear data flow - Context goes through an intermediate ConcurrentHashMap instead of flowing directly
Describe the solution you'd like
I suggest adding an extra field to InboundMessage (or similar mechanism) to allow callers to pass custom context that flows through the chain:
InboundMessage.extra → MsgContext.extra → Gateway → RuntimeContext
Option A: Add extra to InboundMessage
public record InboundMessage(
String channelId,
String accountId,
Peer peer,
String senderId,
Peer parentPeer,
String guild,
String team,
Set<String> roles,
List<Msg> messages,
String preferredAgentId,
Map<String, String> extra // ← New field
) {
// ...
}Then ChannelRouter.buildContext would merge it:
private MsgContext buildContext(InboundMessage msg, DmScope dmScope, String agentId, String userId) {
Map<String, String> extra = new HashMap<>();
extra.put("agentId", agentId);
if (msg.extra() != null) {
extra.putAll(msg.extra()); // ← Merge InboundMessage.extra
}
// ... rest of logic
}Option B: Add extra to MsgContext builder in ChannelRouter
Allow ChannelRouter to accept a context enrichment callback:
public interface ChannelRouter {
RouteResult resolveRoute(InboundMessage msg, BiConsumer<String, String> extraEnricher);
}Describe alternatives you've considered
- ThreadLocal / RequestAttributes - Couples Gateway to Servlet API, not suitable for non-web scenarios
- Storing in Msg.accountId - Overloading existing fields is hacky
- Custom middleware to inject context - Requires knowing the context before dispatch, which defeats the purpose
The ConcurrentHashMap workaround works but is error-prone and feels like fighting the framework rather than working with it.
Additional context
Current architecture:
Controller (extracts modelConfigId, datasourceId)
↓
Service.put(userId, config) ← ConcurrentHashMap
↓
Channel.dispatchStream(inbound)
↓
Gateway.runStream(msgContext)
↓
Gateway.get(userId) ← ConcurrentHashMap
↓
RuntimeContext.put("modelConfigId", ...)
↓
Middleware / Tool reads ctx.get("modelConfigId")
Desired architecture:
Controller (extracts modelConfigId, datasourceId)
↓
Service.buildInbound(extra={modelConfigId, datasourceId})
↓
Channel.dispatchStream(inbound)
↓
Gateway.runStream(msgContext) ← MsgContext.extra contains modelConfigId
↓
RuntimeContext.put("modelConfigId", ctx.extra().get("modelConfigId"))
↓
Middleware / Tool reads ctx.get("modelConfigId")
This would eliminate the need for ModelContext and DatasourceContext helper classes entirely.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
xzxiaoshan commentedon Nov 3, 2026
This has been implemented (and went further than proposed) by #2604:
InboundMessage.runtimeContext()+withRuntimeContext(...)— carry a caller-builtRuntimeContextthroughChannel.dispatchStreaminto the Gateway turnChatUiChannelSendOptions.withRuntimeContext(...)/.putAttribute(key, value)— the Controller-layer entry for per-request business params (tenantId, modelConfigId, …)ChannelRuntimeContextResolver— pluggable hook onHarnessGatewayfor channel adaptersNote the design differs from the proposal: instead of adding
extratoInboundMessageand copying it intoMsgContext.extra, the fullRuntimeContextrides onInboundMessagedirectly —MsgContext.extraparticipates incanonicalKey()(session routing), so business params there would fragment sessions. Gateway identity fields (sessionId,userId, …) still win on conflict. See docs:docs/v2/en/docs/harness/channel.md.Closing as completed — feel free to reopen if anything's missing.