@microsoft/agents-hosting-express package
Interfaces
| CloudAdapterResult |
Result of creating a CloudAdapter from an agent. |
| StartServerOptions |
Options for configuring the Express server started by |
| WebResponse |
Framework-agnostic response surface used by the hosting layer. |
Type Aliases
| AgentRequestHandler |
A request handler function signature that does not import Express types in its public API. |
Functions
| start |
Starts an Express server for handling Agent requests. Example
Example
|
| start |
Variables
| create |
Creates a request handler for processing Agent activities. This exposes a handler signature without requiring Express types in consumer code.
It can be used with Express directly, or with frameworks that provide adapted objects compatible with
the requirements of JWT authorization is applied within the handler before processing the activity.
Requests must provide a parsed activity payload at Example
|
| create |
Creates a CloudAdapter for the given agent. If the agent is an AgentApplication with a pre-configured adapter, that adapter is reused. Otherwise, a new CloudAdapter is created. Example
|
Function Details
startServer(AgentApplication<TurnState<any, any>> | ActivityHandler, StartServerOptions)
Starts an Express server for handling Agent requests.
Example
// Basic usage
import { AgentApplication, TurnState } from '@microsoft/agents-hosting';
import { startServer } from '@microsoft/agents-hosting-express';
const app = new AgentApplication<TurnState>();
startServer(app);
Example
// With options
import { AgentApplication, TurnState } from '@microsoft/agents-hosting';
import { startServer } from '@microsoft/agents-hosting-express';
const app = new AgentApplication<TurnState>();
startServer(app, {
port: 8080,
routePath: '/bot/messages',
beforeListen: (server) => {
server.get('/health', (req, res) => res.json({ status: 'ok' }));
}
});
function startServer(agent: AgentApplication<TurnState<any, any>> | ActivityHandler, options?: StartServerOptions): Express
Parameters
- agent
-
AgentApplication<TurnState<any, any>> | ActivityHandler
The AgentApplication or ActivityHandler instance to process incoming activities.
- options
- StartServerOptions
Optional configuration. Accepts either a StartServerOptions object or
an AuthConfiguration for backward compatibility.
Returns
Express
The Express server instance.
Remarks
This function sets up an Express server with the necessary middleware and routes for handling agent requests. It configures JWT authorization middleware on the messages route and sets up the endpoint. The server will listen on the port specified in options, the PORT environment variable, or 3978 by default.
startServer(AgentApplication<TurnState<any, any>> | ActivityHandler, AuthConfiguration)
function startServer(agent: AgentApplication<TurnState<any, any>> | ActivityHandler, authConfiguration?: AuthConfiguration): Express
Parameters
- agent
-
AgentApplication<TurnState<any, any>> | ActivityHandler
- authConfiguration
- AuthConfiguration
Returns
Express
Variable Details
createAgentRequestHandler
Creates a request handler for processing Agent activities.
This exposes a handler signature without requiring Express types in consumer code.
It can be used with Express directly, or with frameworks that provide adapted objects compatible with
the requirements of authorizeJWT and CloudAdapter.process.
JWT authorization is applied within the handler before processing the activity.
Requests must provide a parsed activity payload at req.body.
Example
import express from 'express';
import { AgentApplication, TurnState } from '@microsoft/agents-hosting';
import { createAgentRequestHandler } from '@microsoft/agents-hosting-express';
const agent = new AgentApplication<TurnState>();
const handler = createAgentRequestHandler(agent);
const app = express();
app.use(express.json());
app.post('/api/messages', handler);
app.get('/health', (req, res) => res.json({ status: 'ok' }));
app.listen(3978);
createAgentRequestHandler: (agent: AgentApplication<TurnState<any, any>> | ActivityHandler, authConfiguration?: AuthConfiguration) => AgentRequestHandler
Type
(agent: AgentApplication<TurnState<any, any>> | ActivityHandler, authConfiguration?: AuthConfiguration) => AgentRequestHandler
createCloudAdapter
Creates a CloudAdapter for the given agent.
If the agent is an AgentApplication with a pre-configured adapter, that adapter is reused. Otherwise, a new CloudAdapter is created.
Example
import { AgentApplication, TurnState, createCloudAdapter } from '@microsoft/agents-hosting';
const app = new AgentApplication<TurnState>();
const { adapter, headerPropagation } = createCloudAdapter(app, { clientId: process.env.CLIENT_ID });
// Use the adapter directly with request/response objects compatible with CloudAdapter.process
adapter.process(req, res, (context) => app.run(context), headerPropagation);
createCloudAdapter: (agent: AgentApplication<TurnState<any, any>> | ActivityHandler, authConfig?: AuthConfiguration) => CloudAdapterResult
Type
(agent: AgentApplication<TurnState<any, any>> | ActivityHandler, authConfig?: AuthConfiguration) => CloudAdapterResult