Definition
Assembly: Microsoft.OData.Mcp.Core.dll Namespace: Microsoft.OData.Mcp.Core.Tools Inheritance: System.ObjectSyntax
Summary
Provides execution context for MCP tool operations.Remarks
This class encapsulates the runtime context needed for tool execution, including user identity, request metadata, and service dependencies.Constructors
.ctor
Initializes a new instance of the McpToolContext class.Syntax
.ctor
Initializes a new instance of the McpToolContext class with the specified model.Syntax
Parameters
| Name | Type | Description |
|---|---|---|
model | Microsoft.OData.Mcp.Core.Models.EdmModel | The OData model for this context. |
Exceptions
| Exception | Description |
|---|---|
ArgumentNullException | Thrown when model is null. |
.ctor Inherited
Inherited from
objectSyntax
Properties
AuthToken
Gets or sets the authentication token to forward to the OData service.Syntax
Property Value
Type:string?
The bearer token to include in OData service requests.
Remarks
This token is forwarded to the OData service when the tool performs operations that require authentication. The token delegation strategy determines how this token is processed.CancellationToken
Gets or sets the cancellation token for this request.Syntax
Property Value
Type:System.Threading.CancellationToken?
Token for cancelling long-running operations.
Remarks
Tools should respect this token and cancel operations appropriately when cancellation is requested.CorrelationId
Gets or sets the correlation ID for this request.Syntax
Property Value
Type:string
A unique identifier for tracking this request across systems.
Remarks
The correlation ID is used for logging, tracing, and debugging purposes. It should be propagated to downstream services for end-to-end tracking.HttpClientFactory
Gets or sets the HTTP client factory for making service requests.Syntax
Property Value
Type:System.Net.Http.IHttpClientFactory?
Factory for creating HTTP clients with proper configuration.
Remarks
Used to create HTTP clients for communicating with the OData service when authentication token delegation is required.MaxExecutionTime
Gets or sets the maximum allowed execution time for the tool.Syntax
Property Value
Type:System.TimeSpan?
The maximum time the tool is allowed to execute.
Remarks
This provides a safety mechanism to prevent tools from running indefinitely. Tools should monitor their execution time and stop gracefully when approaching this limit.Model
Gets or sets the OData model for this execution context.Syntax
Property Value
Type:Microsoft.OData.Mcp.Core.Models.EdmModel
The complete OData model providing metadata context.
Remarks
The model contains all entity types, properties, relationships, and other metadata needed for tool execution and validation.Properties
Gets or sets custom properties for tool execution.Syntax
Property Value
Type:System.Collections.Generic.Dictionary<string, object>
A dictionary of custom key-value pairs for tool-specific data.
Remarks
This allows tools to store and retrieve custom context information that may be needed during execution.RequestTimestamp
Gets or sets the request timestamp.Syntax
Property Value
Type:System.DateTime?
The UTC timestamp when the tool request was initiated.
Remarks
Used for auditing, performance tracking, and timeout calculations.ServiceBaseUrl
Gets or sets the base URL of the OData service.Syntax
Property Value
Type:string?
The base URL used for constructing OData requests.
Remarks
This URL is used when the tool needs to make HTTP requests to the underlying OData service for CRUD operations and queries.User
Gets or sets the user identity making the request.Syntax
Property Value
Type:System.Security.Claims.ClaimsPrincipal?
The claims principal representing the authenticated user, or null for anonymous requests.
Remarks
This contains all claims and identity information from the JWT token, including user ID, scopes, roles, and other custom claims.Methods
CreateServiceHttpClient
Creates an HTTP client for making requests to the OData service.Syntax
Returns
Type:System.Net.Http.HttpClient?
An HTTP client configured for OData service requests, or null if no factory is available.
Remarks
The returned client will include the authentication token if available. Callers are responsible for disposing the client.Equals Inherited Virtual
Inherited from
objectSyntax
Parameters
| Name | Type | Description |
|---|---|---|
obj | object? | - |
Returns
Type:bool
Equals Inherited
Inherited from
objectSyntax
Parameters
| Name | Type | Description |
|---|---|---|
objA | object? | - |
objB | object? | - |
Returns
Type:bool
GetHashCode Inherited Virtual
Inherited from
objectSyntax
Returns
Type:int
GetProperty
Gets a custom property value.Syntax
Parameters
| Name | Type | Description |
|---|---|---|
key | string | The property key. |
Returns
Type:T?
The property value if found and of the correct type; otherwise, the default value.
Type Parameters
T- The type of the property value.
GetRemainingExecutionTime
Gets the remaining execution time.Syntax
Returns
Type:System.TimeSpan?
The remaining time before the execution limit is reached.
GetType Inherited
Inherited from
objectSyntax
Returns
Type:System.Type
GetUserEmail
Gets the user’s email from the claims.Syntax
Returns
Type:string?
The user’s email address, or null if not available.
GetUserId
Gets the user identifier from the claims.Syntax
Returns
Type:string?
The user identifier, or null if not available.
GetUserName
Gets the user’s display name from the claims.Syntax
Returns
Type:string?
The user’s display name, or null if not available.
GetUserRoles
Gets the user’s roles from the claims.Syntax
Returns
Type:System.Collections.Generic.IEnumerable<string>
A collection of user roles, or empty if no roles are available.
GetUserScopes
Gets the user’s OAuth2 scopes from the claims.Syntax
Returns
Type:System.Collections.Generic.IEnumerable<string>
A collection of OAuth2 scopes, or empty if no scopes are available.
HasAnyScope
Determines whether the user has any of the specified scopes.Syntax
Parameters
| Name | Type | Description |
|---|---|---|
scopes | System.Collections.Generic.IEnumerable<string> | The scopes to check for. |
Returns
Type:bool
true if the user has any of the specified scopes; otherwise, false.
HasRole
Determines whether the user has the specified role.Syntax
Parameters
| Name | Type | Description |
|---|---|---|
role | string | The role to check for. |
Returns
Type:bool
true if the user has the specified role; otherwise, false.
HasScope
Determines whether the user has the specified scope.Syntax
Parameters
| Name | Type | Description |
|---|---|---|
scope | string | The scope to check for. |
Returns
Type:bool
true if the user has the specified scope; otherwise, false.
IsExecutionTimeLimitExceeded
Checks if the execution time limit has been exceeded.Syntax
Returns
Type:bool
true if the execution time limit has been exceeded; otherwise, false.
MemberwiseClone Inherited
Inherited from
objectSyntax
Returns
Type:object
ReferenceEquals Inherited
Inherited from
objectSyntax
Parameters
| Name | Type | Description |
|---|---|---|
objA | object? | - |
objB | object? | - |
Returns
Type:bool
SetProperty
Sets a custom property value.Syntax
Parameters
| Name | Type | Description |
|---|---|---|
key | string | The property key. |
value | object | The property value. |
Exceptions
| Exception | Description |
|---|---|
ArgumentException | Thrown when key is null or whitespace. |
ToString Inherited Virtual
Inherited from
objectSyntax
Returns
Type:string?