Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Copyright (c) Microsoft. All rights reserved.
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

from .environment_utils import get_observability_authentication_scope
from .operation_error import OperationError
from .operation_result import OperationResult
from .power_platform_api_discovery import ClusterCategory, PowerPlatformApiDiscovery
from .utility import Utility

Expand All @@ -9,6 +12,8 @@
"PowerPlatformApiDiscovery",
"ClusterCategory",
"Utility",
"OperationError",
"OperationResult",
]

__path__ = __import__("pkgutil").extend_path(__path__, __name__)
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""
Encapsulates an error from an operation.
"""


class OperationError:
"""
Represents an error that occurred during an operation.

This class wraps an exception and provides a consistent interface for
accessing error information.
"""

def __init__(self, exception: Exception):
"""
Initialize a new instance of the OperationError class.

Args:
exception: The exception associated with the error.

Raises:
ValueError: If exception is None.
"""
if exception is None:
raise ValueError("exception cannot be None")
self._exception = exception

@property
def exception(self) -> Exception:
"""
Get the exception associated with the error.

Returns:
Exception: The exception associated with the error.
"""
return self._exception

@property
def message(self) -> str:
"""
Get the message associated with the error.

Returns:
str: The error message from the exception.
"""
return str(self._exception)

def __str__(self) -> str:
"""
Return a string representation of the error.

Returns:
str: A string representation of the error.
"""
return str(self._exception)
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""
Represents the result of an operation.
"""

from typing import List, Optional

from .operation_error import OperationError


class OperationResult:
"""
Represents the result of an operation.

This class encapsulates the success or failure state of an operation along
with any associated errors.
"""

_success_instance: Optional["OperationResult"] = None

def __init__(self, succeeded: bool, errors: Optional[List[OperationError]] = None):
"""
Initialize a new instance of the OperationResult class.

Args:
succeeded: Flag indicating whether the operation succeeded.
errors: Optional list of errors that occurred during the operation.
"""
self._succeeded = succeeded
self._errors = errors if errors is not None else []

@property
def succeeded(self) -> bool:
"""
Get a flag indicating whether the operation succeeded.

Returns:
bool: True if the operation succeeded, otherwise False.
"""
return self._succeeded

@property
def errors(self) -> List[OperationError]:
"""
Get the list of errors that occurred during the operation.

Returns:
List[OperationError]: List of operation errors.
"""
return self._errors

@staticmethod
def success() -> "OperationResult":
"""
Return an OperationResult indicating a successful operation.

Returns:
OperationResult: An OperationResult indicating a successful operation.
"""
if OperationResult._success_instance is None:
OperationResult._success_instance = OperationResult(succeeded=True)
return OperationResult._success_instance

@staticmethod
def failed(*errors: OperationError) -> "OperationResult":
"""
Create an OperationResult indicating a failed operation.

Args:
*errors: Variable number of OperationError instances.

Returns:
OperationResult: An OperationResult indicating a failed operation.
"""
error_list = list(errors) if errors else []
return OperationResult(succeeded=False, errors=error_list)

def __str__(self) -> str:
"""
Convert the value of the current OperationResult object to its string representation.

Returns:
str: A string representation of the current OperationResult object.
"""
if self._succeeded:
return "Succeeded"
else:
error_messages = ", ".join(str(error.message) for error in self._errors)
return f"Failed : {error_messages}"
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Copyright (c) Microsoft. All rights reserved.
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""
Common models for MCP tooling.

This module defines data models used across the MCP tooling framework.
"""

from .chat_history_message import ChatHistoryMessage
from .chat_message_request import ChatMessageRequest
from .mcp_server_config import MCPServerConfig
from .tool_options import ToolOptions

__all__ = ["MCPServerConfig", "ToolOptions"]
__all__ = ["MCPServerConfig", "ToolOptions", "ChatHistoryMessage", "ChatMessageRequest"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""
Chat History Message model.
"""

from dataclasses import dataclass
from datetime import datetime


@dataclass
class ChatHistoryMessage:
"""
Represents a single message in the chat history.

This class is used to send chat history to the MCP platform for real-time
threat protection analysis.
"""

#: The unique identifier for the chat message.
id: str

#: The role of the message sender (e.g., "user", "assistant", "system").
role: str

#: The content of the chat message.
content: str

#: The timestamp of when the message was sent.
timestamp: datetime

def __post_init__(self):
"""Validate the message after initialization."""
if not self.id:
raise ValueError("id cannot be empty")
if not self.role:
raise ValueError("role cannot be empty")
if not self.content:
raise ValueError("content cannot be empty")
if not self.timestamp:
raise ValueError("timestamp cannot be empty")

def to_dict(self):
"""
Convert the message to a dictionary for JSON serialization.

Returns:
dict: Dictionary representation of the message.
"""
return {
"id": self.id,
"role": self.role,
"content": self.content,
"timestamp": self.timestamp.isoformat(),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""
Chat Message Request model.
"""

from dataclasses import dataclass
from typing import List

from .chat_history_message import ChatHistoryMessage


@dataclass
class ChatMessageRequest:
"""
Represents the request payload for a real-time threat protection check on a chat message.

This class encapsulates the information needed to send chat history to the MCP platform
for threat analysis.
"""

#: The unique identifier for the conversation.
conversation_id: str

#: The unique identifier for the message within the conversation.
message_id: str

#: The content of the user's message.
user_message: str

#: The chat history messages.
chat_history: List[ChatHistoryMessage]

def __post_init__(self):
"""Validate the request after initialization."""
if not self.conversation_id:
raise ValueError("conversation_id cannot be empty")
if not self.message_id:
raise ValueError("message_id cannot be empty")
if not self.user_message:
raise ValueError("user_message cannot be empty")
if not self.chat_history:
raise ValueError("chat_history cannot be empty")

def to_dict(self):
"""
Convert the request to a dictionary for JSON serialization.

Returns:
dict: Dictionary representation of the request.
"""
return {
"conversationId": self.conversation_id,
"messageId": self.message_id,
"userMessage": self.user_message,
"chatHistory": [msg.to_dict() for msg in self.chat_history],
}
Loading
Loading