Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ddf4b6e
add azure-ai-agentserver-core
lusu-msft Nov 3, 2025
3c8c265
ignore non-exist namespace
lusu-msft Nov 3, 2025
ad1b592
try fix version
lusu-msft Nov 3, 2025
9aef09f
try fix version
lusu-msft Nov 3, 2025
f238c60
try build with updated dependency
lusu-msft Nov 4, 2025
0939aec
fix typo
lusu-msft Nov 4, 2025
31d59b2
try debug build
lusu-msft Nov 4, 2025
b07370f
try debug build
lusu-msft Nov 4, 2025
5059963
try enable build by changing required python version
lusu-msft Nov 4, 2025
1f70e4e
try build with 3.10
lusu-msft Nov 4, 2025
3a120dd
revert ai matrix setting
lusu-msft Nov 4, 2025
94606a6
override azure-ai-agentserver-core required python version to enable …
lusu-msft Nov 4, 2025
253c119
remove unused project files
lusu-msft Nov 4, 2025
5822186
fix init.py
lusu-msft Nov 4, 2025
e897e6f
fix dependency
lusu-msft Nov 4, 2025
070ed65
try fix pylint
lusu-msft Nov 4, 2025
491f5c4
try fix pylint
lusu-msft Nov 4, 2025
5533774
add pytest cases
lusu-msft Nov 4, 2025
7533353
revert eng changes
lusu-msft Nov 4, 2025
be5612d
add azure-ai-agentserver-langgraph package
lusu-msft Nov 4, 2025
92e7443
try fix sphinx
lusu-msft Nov 4, 2025
0303735
Merge branch 'main' into lusu/agentserver-core
lusu-msft Nov 4, 2025
9f516a5
fix format
lusu-msft Nov 4, 2025
e4e0eac
fix dependency check
lusu-msft Nov 4, 2025
d980ecc
fix sphinx build
lusu-msft Nov 4, 2025
c7d1b4c
fix pylint
lusu-msft Nov 5, 2025
15d1b3b
fix proj
lusu-msft Nov 5, 2025
3e9dfb5
update proj
lusu-msft Nov 5, 2025
ec75aed
fix format
lusu-msft Nov 5, 2025
76b0fe3
remove index.md
lusu-msft Nov 5, 2025
cb7e4df
Merge branch 'lusu/agentserver-core' into lusu/agentserver-langgraph
lusu-msft Nov 5, 2025
319f394
fix pylint
lusu-msft Nov 5, 2025
d725470
update pyproject.toml
lusu-msft Nov 5, 2025
e39b638
add manifest
lusu-msft Nov 5, 2025
920d262
fix sphinx
lusu-msft Nov 5, 2025
d302ab2
fix format, synced
lusu-msft Nov 5, 2025
ab8660d
fix mindependency
lusu-msft Nov 5, 2025
5d82677
fix ci.yml
lusu-msft Nov 5, 2025
58268f6
update test case
lusu-msft Nov 5, 2025
3c5dd4f
Merge branch 'lusu/agentserver-core' into lusu/agentserver-langgraph
lusu-msft Nov 5, 2025
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
6 changes: 5 additions & 1 deletion eng/tools/azure-sdk-tools/ci_tools/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@
"sdk/textanalytics/azure-ai-textanalytics",
]

TEST_COMPATIBILITY_MAP = {"azure-ai-ml": ">=3.7"}
TEST_COMPATIBILITY_MAP = {
"azure-ai-ml": ">=3.7",
"azure-ai-agentserver-core": ">=3.9", # override to allow build with python 3.9
"azure-ai-agentserver-langgraph": ">=3.9", # override to allow build with python 3.9
}
TEST_PYTHON_DISTRO_INCOMPATIBILITY_MAP = {
"azure-storage-blob": "pypy",
"azure-storage-queue": "pypy",
Expand Down
7 changes: 7 additions & 0 deletions sdk/ai/azure-ai-agentserver-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Release History

## 1.0.0

### Features Added

First version
8 changes: 8 additions & 0 deletions sdk/ai/azure-ai-agentserver-core/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
include *.md
include LICENSE
recursive-include tests *.py
recursive-include samples *.py *.md
recursive-include doc *.rst *.md
include azure/__init__.py
include azure/ai/__init__.py
include azure/ai/agentserver/__init__.py
78 changes: 78 additions & 0 deletions sdk/ai/azure-ai-agentserver-core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Azure AI Agent Server Adapter

## Install

In current folder, run:
```bash
pip install -e .
```

## Usage

If your agent is not built using a supported framework such as LangGraph and Agent-framework, you can still make it compatible with Microsoft AI Foundry by manually implementing the predefined interface.

```python
import datetime

from azure.ai.agentserver.core import FoundryCBAgent
from azure.ai.agentserver.core.models import (
CreateResponse,
Response as OpenAIResponse,
)
from azure.ai.agentserver.core.models.projects import (
ItemContentOutputText,
ResponsesAssistantMessageItemResource,
ResponseTextDeltaEvent,
ResponseTextDoneEvent,
)


def stream_events(text: str):
assembled = ""
for i, token in enumerate(text.split(" ")):
piece = token if i == len(text.split(" ")) - 1 else token + " "
assembled += piece
yield ResponseTextDeltaEvent(delta=piece)
# Done with text
yield ResponseTextDoneEvent(text=assembled)


async def agent_run(request_body: CreateResponse):
agent = request_body.agent
print(f"agent:{agent}")

if request_body.stream:
return stream_events("I am mock agent with no intelligence in stream mode.")

# Build assistant output content
output_content = [
ItemContentOutputText(
text="I am mock agent with no intelligence.",
annotations=[],
)
]

response = OpenAIResponse(
metadata={},
temperature=0.0,
top_p=0.0,
user="me",
id="id",
created_at=datetime.datetime.now(),
output=[
ResponsesAssistantMessageItemResource(
status="completed",
content=output_content,
)
],
)
return response


my_agent = FoundryCBAgent()
my_agent.agent_run = agent_run

if __name__ == "__main__":
my_agent.run()

```
1 change: 1 addition & 0 deletions sdk/ai/azure-ai-agentserver-core/azure/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore
1 change: 1 addition & 0 deletions sdk/ai/azure-ai-agentserver-core/azure/ai/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
__path__ = __import__("pkgutil").extend_path(__path__, __name__)

from ._version import VERSION
from .logger import configure as config_logging
from .server.base import FoundryCBAgent
from .server.common.agent_run_context import AgentRunContext

config_logging()

__all__ = ["FoundryCBAgent", "AgentRunContext"]
__version__ = VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# Code generated by Microsoft (R) Python Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

VERSION = "1.0.0a1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
class Constants:
# well-known environment variables
APPLICATION_INSIGHTS_CONNECTION_STRING = "_AGENT_RUNTIME_APP_INSIGHTS_CONNECTION_STRING"
AZURE_AI_PROJECT_ENDPOINT = "AZURE_AI_PROJECT_ENDPOINT"
AGENT_ID = "AGENT_ID"
AGENT_NAME = "AGENT_NAME"
AGENT_PROJECT_RESOURCE_ID = "AGENT_PROJECT_NAME"
OTEL_EXPORTER_ENDPOINT = "OTEL_EXPORTER_ENDPOINT"
AGENT_LOG_LEVEL = "AGENT_LOG_LEVEL"
AGENT_DEBUG_ERRORS = "AGENT_DEBUG_ERRORS"
ENABLE_APPLICATION_INSIGHTS_LOGGER = "ENABLE_APPLICATION_INSIGHTS_LOGGER"
159 changes: 159 additions & 0 deletions sdk/ai/azure-ai-agentserver-core/azure/ai/agentserver/core/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# pylint: disable=broad-exception-caught,dangerous-default-value
# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
import contextvars
import logging
import os
from logging import config

from ._version import VERSION
from .constants import Constants

default_log_config = {
"version": 1,
"disable_existing_loggers": False,
"loggers": {
"azure.ai.agentshosting": {
"handlers": ["console"],
"level": "INFO",
"propagate": False,
},
},
"handlers": {
"console": {"formatter": "std_out", "class": "logging.StreamHandler", "level": "INFO"},
},
"formatters": {"std_out": {"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"}},
}

request_context = contextvars.ContextVar("request_context", default=None)


def get_dimensions():
env_values = {name: value for name, value in vars(Constants).items() if not name.startswith("_")}
res = {"azure.ai.agentshosting.version": VERSION}
for name, env_name in env_values.items():
if isinstance(env_name, str) and not env_name.startswith("_"):
runtime_value = os.environ.get(env_name)
if runtime_value:
res[f"azure.ai.agentshosting.{name.lower()}"] = runtime_value
return res


def get_project_endpoint():
project_resource_id = os.environ.get(Constants.AGENT_PROJECT_RESOURCE_ID)
if project_resource_id:
last_part = project_resource_id.split("/")[-1]

parts = last_part.split("@")
if len(parts) < 2:
print(f"invalid project resource id: {project_resource_id}")
return None
account = parts[0]
project = parts[1]
return f"https://{account}.services.ai.azure.com/api/projects/{project}"
print("environment variable AGENT_PROJECT_RESOURCE_ID not set.")
return None


def get_application_insights_connstr():
try:
conn_str = os.environ.get(Constants.APPLICATION_INSIGHTS_CONNECTION_STRING)
if not conn_str:
print("environment variable APPLICATION_INSIGHTS_CONNECTION_STRING not set.")
project_endpoint = get_project_endpoint()
if project_endpoint:
# try to get the project connected application insights
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential

project_client = AIProjectClient(credential=DefaultAzureCredential(), endpoint=project_endpoint)
conn_str = project_client.telemetry.get_application_insights_connection_string()
if not conn_str:
print(f"no connected application insights found for project:{project_endpoint}")
else:
os.environ[Constants.APPLICATION_INSIGHTS_CONNECTION_STRING] = conn_str
return conn_str
except Exception as e:
print(f"failed to get application insights with error: {e}")
return None


class CustomDimensionsFilter(logging.Filter):
def filter(self, record):
# Add custom dimensions to every log record
dimensions = get_dimensions()
for key, value in dimensions.items():
setattr(record, key, value)
cur_request_context = request_context.get()
if cur_request_context:
for key, value in cur_request_context.items():
setattr(record, key, value)
return True


def configure(log_config: dict = default_log_config):
"""
Configure logging based on the provided configuration dictionary.
The dictionary should contain the logging configuration in a format compatible with `logging.config.dictConfig`.
:param log_config: A dictionary containing logging configuration.
:type log_config: dict
"""
try:
config.dictConfig(log_config)

application_insights_connection_string = get_application_insights_connstr()
enable_application_insights_logger = (
os.environ.get(Constants.ENABLE_APPLICATION_INSIGHTS_LOGGER, "true").lower() == "true"
)
if application_insights_connection_string and enable_application_insights_logger:
from opentelemetry._logs import set_logger_provider
from opentelemetry.sdk._logs import (
LoggerProvider,
LoggingHandler,
)
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
from opentelemetry.sdk.resources import Resource

from azure.monitor.opentelemetry.exporter import AzureMonitorLogExporter

logger_provider = LoggerProvider(resource=Resource.create({"service.name": "azure.ai.agentshosting"}))
set_logger_provider(logger_provider)

exporter = AzureMonitorLogExporter(connection_string=application_insights_connection_string)

logger_provider.add_log_record_processor(BatchLogRecordProcessor(exporter))
handler = LoggingHandler(logger_provider=logger_provider)
handler.name = "appinsights_handler"

# Add custom filter to inject dimensions
custom_filter = CustomDimensionsFilter()
handler.addFilter(custom_filter)

# Only add to azure.ai.agentshosting namespace to avoid infrastructure logs
app_logger = logging.getLogger("azure.ai.agentshosting")
app_logger.setLevel(get_log_level())
app_logger.addHandler(handler)

except Exception as e:
print(f"Failed to configure logging: {e}")


def get_log_level():
log_level = os.getenv(Constants.AGENT_LOG_LEVEL, "INFO").upper()
valid_levels = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
if log_level not in valid_levels:
print(f"Invalid log level '{log_level}' specified. Defaulting to 'INFO'.")
log_level = "INFO"
return log_level


def get_logger() -> logging.Logger:
"""
If the logger is not already configured, it will be initialized with default settings.
:return: Configured logger instance.
:rtype: logging.Logger
"""
return logging.getLogger("azure.ai.agentshosting")
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
from ._create_response import CreateResponse # type: ignore
from .projects import Response, ResponseStreamEvent

__all__ = ["CreateResponse", "Response", "ResponseStreamEvent"] # type: ignore[var-annotated]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
from typing import Optional

from .openai import response_create_params #pylint: disable=no-name-in-module
from . import projects as _azure_ai_projects_models

class CreateResponse(response_create_params.ResponseCreateParamsBase, total=False):
agent: Optional[_azure_ai_projects_models.AgentReference]
stream: Optional[bool]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
"""
Re-exports of OpenAI SDK response types.
This module re-exports types from the OpenAI SDK for convenience.
These types are fully documented in the OpenAI SDK documentation.
.. note::
This module re-exports OpenAI SDK types. For detailed documentation,
please refer to the `OpenAI Python SDK documentation <https://github.com/openai/openai-python>`_.
"""
from openai.types.responses import * # pylint: disable=unused-wildcard-import

__all__ = [name for name in globals() if not name.startswith("_")] # type: ignore[var-annotated]
Loading
Loading