Examples
This page provides complete, runnable examples demonstrating how to use the Working Context package in various scenarios. Each example includes all necessary imports and shows how to configure the schema, initialize the Working Context, and interact with contextual data.
Tracking User Profile
This example demonstrates tracking basic user information from a global context. The configuration extracts user identification, full name, and role from the UserSession global context.
import { IoIntelWorkingContextFactory } from "@interopio/working-context";
import IOConnectBrowser from "@interopio/browser";
// Initialize io.Connect
const io = await IOConnectBrowser();
// Configure the Working Context schema
const config = {
schema: {
userId: {
type: "string",
description: "Unique user identifier",
source: {
context: {
location: { global: { names: ["UserSession"] } },
path: "user.id",
},
},
},
userName: {
type: "string",
description: "User's full name",
source: {
context: {
location: { global: { names: ["UserSession"] } },
path: "user.fullName",
},
},
},
userRole: {
type: "string",
description: "User's role in the organization",
source: {
context: {
location: { global: { names: ["UserSession"] } },
path: "user.role",
},
},
},
},
};
// Initialize Working Context
const workingContext = await IoIntelWorkingContextFactory(io, config);
// Access user information
const context = workingContext.get();
console.log(`Welcome, ${context.userName.value}!`);
console.log(`User ID: ${context.userId.value}`);
console.log(`Role: ${context.userRole.value}`);
Multi-Source Trading Application
This example demonstrates a more complex scenario where contextual data is gathered from multiple sources: global context, workspace context, channel data, and application instance context. This pattern is typical in financial trading applications where information flows from various components.
import { IoIntelWorkingContextFactory } from "@interopio/working-context";
import IOConnectBrowser from "@interopio/browser";
// Initialize io.Connect with Workspaces support
const io = await IOConnectBrowser({
workspaces: true,
});
// Configure multi-source schema
const tradingConfig = {
schema: {
// Track trader ID from global context
traderId: {
type: "string",
description: "Unique trader identifier",
source: {
context: {
location: { global: { names: ["TraderProfile"] } },
path: "trader.id",
},
},
},
// Track active instrument from workspace context
activeInstrument: {
type: "object",
description: "Currently selected trading instrument",
source: {
context: {
location: { workspace: { target: "my" } },
path: "instrument",
},
},
},
// Track real-time market data from channel
marketData: {
type: "object",
description: "Real-time market data",
source: {
context: {
location: { channel: { target: "MarketData" } },
path: "data",
},
},
},
// Track open orders from application instance
openOrders: {
type: "array",
description: "List of open orders",
source: {
context: {
location: { appInstance: { appNames: ["OrderManager"] } },
path: "orders.open",
},
},
},
},
};
// Initialize Working Context
const tradingContext = await IoIntelWorkingContextFactory(io, tradingConfig);
// Get initial context state
const initialContext = tradingContext.get();
console.log("Initial trading context:", initialContext);
// Monitor trading context changes
const unsubscribe = tradingContext.onChanged((context) => {
// React to instrument changes
if (context.activeInstrument.value) {
console.log(
`Instrument changed to: ${context.activeInstrument.value.symbol}`
);
console.log(`Price: ${context.activeInstrument.value.price}`);
}
// React to market data updates
if (context.marketData.value) {
console.log("Market data updated:", context.marketData.value);
}
// Monitor open orders
if (context.openOrders.value) {
console.log(`Open orders count: ${context.openOrders.value.length}`);
// Check for filled orders
const filledOrders = context.openOrders.value.filter(
order => order.status === "filled"
);
if (filledOrders.length > 0) {
console.log("Orders filled:", filledOrders);
}
}
});
// Later, stop listening to changes
// unsubscribe();
Deeply Nested Data Paths
This example demonstrates how to access deeply nested properties within context objects using dot notation. The Working Context package automatically traverses the object hierarchy to extract values at any depth.
import { IoIntelWorkingContextFactory } from "@interopio/working-context";
import IOConnectBrowser from "@interopio/browser";
const io = await IOConnectBrowser();
// Configure schema with deeply nested paths
const nestedConfig = {
schema: {
departmentName: {
type: "string",
description: "Department name from organizational hierarchy",
source: {
context: {
location: { global: { names: ["OrgStructure"] } },
// Traverse through organization -> division -> department -> name
path: "organization.division.department.name",
},
},
},
budgetRemaining: {
type: "number",
description: "Remaining budget for current fiscal year",
source: {
context: {
location: { global: { names: ["FinanceData"] } },
// Traverse through department -> finance -> budget -> fiscal2024 -> remaining
path: "department.finance.budget.fiscal2024.remaining",
},
},
},
primaryContactEmail: {
type: "string",
description: "Email of department's primary contact",
source: {
context: {
location: { global: { names: ["OrgStructure"] } },
path: "organization.division.department.contacts.primary.email",
},
},
},
approvalChain: {
type: "array",
description: "List of approvers in the chain",
source: {
context: {
location: { global: { names: ["WorkflowConfig"] } },
path: "approvals.purchasing.chain.approvers",
},
},
},
},
};
// Initialize Working Context
const workingContext = await IoIntelWorkingContextFactory(io, nestedConfig);
// Access deeply nested data
const context = workingContext.get();
console.log(`Department: ${context.departmentName.value}`);
console.log(`Budget Remaining: $${context.budgetRemaining.value}`);
console.log(`Contact: ${context.primaryContactEmail.value}`);
console.log(`Approval Chain: ${context.approvalChain.value.join(" -> ")}`);
Integration Options
The Working Context package can be integrated into your application in three different ways, depending on your needs.
Standalone Usage
Use the package directly via its API for custom implementations. This approach gives you complete control over context management and is ideal when building custom solutions.
import { IoIntelWorkingContextFactory } from "@interopio/working-context";
import IOConnectBrowser from "@interopio/browser";
async function initializeContext() {
// Initialize io.Connect
const io = await IOConnectBrowser();
// Define your schema
const config = {
schema: {
userName: {
type: "string",
source: {
context: {
location: { global: { names: ["UserProfile"] } },
path: "user.name",
},
},
},
activeDocument: {
type: "object",
source: {
context: {
location: { workspace: { target: "my" } },
path: "document.current",
},
},
},
},
};
// Initialize Working Context
const workingContext = await IoIntelWorkingContextFactory(io, config);
// Get current context
const context = workingContext.get();
console.log("Current context:", context);
// Subscribe to changes
const unsubscribe = workingContext.onChanged((data) => {
// Handle context updates in your custom logic
processContextUpdate(data);
});
return { workingContext, unsubscribe };
}
function processContextUpdate(data) {
// Your custom processing logic
console.log("Context updated:", data);
// Update UI, trigger workflows, notify services, etc.
if (data.activeDocument.value) {
updateDocumentPanel(data.activeDocument.value);
}
}
Integration with @interopio/mcp-core
The @interopio/mcp-core integration is designed for solutions using only the io.Intelligence Model Context Protocol (MCP) without other io.Intelligence packages.
Features:
- Working Context exposed via dedicated MCP tool
- Direct integration with MCP-compatible LLMs
- Lightweight solution for MCP-only architectures
Requirements:
- Custom prompt engineering (developer manages LLM system prompts)
- Understanding of MCP protocol and tool usage
- Manual configuration of LLM context windows
Use Case: MCP-only solutions, custom LLM integrations, or when you need direct control over the MCP tool interface
For implementation details, refer to the MCP Core documentation.