Skip to main content

Definition

Assembly: Microsoft.OData.Mcp.Core.dll Namespace: Microsoft.OData.Mcp.Core.Tools Inheritance: System.Object

Syntax

Microsoft.OData.Mcp.Core.Tools.McpToolContext

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

public McpToolContext()

.ctor

Initializes a new instance of the McpToolContext class with the specified model.

Syntax

public McpToolContext(Microsoft.OData.Mcp.Core.Models.EdmModel model)

Parameters

NameTypeDescription
modelMicrosoft.OData.Mcp.Core.Models.EdmModelThe OData model for this context.

Exceptions

ExceptionDescription
ArgumentNullExceptionThrown when model is null.

.ctor Inherited

Inherited from object

Syntax

public Object()

Properties

AuthToken

Gets or sets the authentication token to forward to the OData service.

Syntax

public string AuthToken { get; set; }

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

public System.Threading.CancellationToken CancellationToken { get; set; }

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

public string CorrelationId { get; set; }

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

public System.Net.Http.IHttpClientFactory HttpClientFactory { get; set; }

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

public System.TimeSpan MaxExecutionTime { get; set; }

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

public required Microsoft.OData.Mcp.Core.Models.EdmModel Model { get; set; }

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

public System.Collections.Generic.Dictionary<string, object> Properties { get; set; }

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

public System.DateTime RequestTimestamp { get; set; }

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

public string ServiceBaseUrl { get; set; }

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

public System.Security.Claims.ClaimsPrincipal User { get; set; }

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

public System.Net.Http.HttpClient CreateServiceHttpClient()

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 object

Syntax

public virtual bool Equals(object obj)

Parameters

NameTypeDescription
objobject?-

Returns

Type: bool

Equals Inherited

Inherited from object

Syntax

public static bool Equals(object objA, object objB)

Parameters

NameTypeDescription
objAobject?-
objBobject?-

Returns

Type: bool

GetHashCode Inherited Virtual

Inherited from object

Syntax

public virtual int GetHashCode()

Returns

Type: int

GetProperty

Gets a custom property value.

Syntax

public T GetProperty<T>(string key)

Parameters

NameTypeDescription
keystringThe 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

public System.TimeSpan GetRemainingExecutionTime()

Returns

Type: System.TimeSpan? The remaining time before the execution limit is reached.

GetType Inherited

Inherited from object

Syntax

public System.Type GetType()

Returns

Type: System.Type

GetUserEmail

Gets the user’s email from the claims.

Syntax

public string GetUserEmail()

Returns

Type: string? The user’s email address, or null if not available.

GetUserId

Gets the user identifier from the claims.

Syntax

public string GetUserId()

Returns

Type: string? The user identifier, or null if not available.

GetUserName

Gets the user’s display name from the claims.

Syntax

public string GetUserName()

Returns

Type: string? The user’s display name, or null if not available.

GetUserRoles

Gets the user’s roles from the claims.

Syntax

public System.Collections.Generic.IEnumerable<string> GetUserRoles()

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

public System.Collections.Generic.IEnumerable<string> GetUserScopes()

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

public bool HasAnyScope(System.Collections.Generic.IEnumerable<string> scopes)

Parameters

NameTypeDescription
scopesSystem.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

public bool HasRole(string role)

Parameters

NameTypeDescription
rolestringThe 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

public bool HasScope(string scope)

Parameters

NameTypeDescription
scopestringThe 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

public bool IsExecutionTimeLimitExceeded()

Returns

Type: bool true if the execution time limit has been exceeded; otherwise, false.

MemberwiseClone Inherited

Inherited from object

Syntax

protected internal object MemberwiseClone()

Returns

Type: object

ReferenceEquals Inherited

Inherited from object

Syntax

public static bool ReferenceEquals(object objA, object objB)

Parameters

NameTypeDescription
objAobject?-
objBobject?-

Returns

Type: bool

SetProperty

Sets a custom property value.

Syntax

public void SetProperty(string key, object value)

Parameters

NameTypeDescription
keystringThe property key.
valueobjectThe property value.

Exceptions

ExceptionDescription
ArgumentExceptionThrown when key is null or whitespace.

ToString Inherited Virtual

Inherited from object

Syntax

public virtual string ToString()

Returns

Type: string?