Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
41 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
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
58268f6
update test case
lusu-msft Nov 5, 2025
d9b97bd
add license
lusu-msft Nov 5, 2025
5e6a254
remove unused requirement
lusu-msft Nov 5, 2025
3e8bcf0
try fix build
lusu-msft Nov 6, 2025
5492d7a
try fix build
lusu-msft Nov 6, 2025
a96d3db
add agent id to trace context
lusu-msft Nov 6, 2025
1f32af7
try fix cspell fail
lusu-msft Nov 6, 2025
93369cb
try fix cspell fail
lusu-msft Nov 6, 2025
128ec2d
fix build
lusu-msft Nov 6, 2025
a7c1107
fix cspell
lusu-msft Nov 6, 2025
6ff3420
update doc header rule
lusu-msft Nov 6, 2025
5f36ad4
add shared dependency
lusu-msft Nov 6, 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
2 changes: 1 addition & 1 deletion eng/tox/run_sphinx_apidoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def sphinx_apidoc(working_directory: str, namespace: str) -> None:
while namespace:
rst_file_to_delete = base_path / f"{namespace}.rst"
logging.info(f"Removing {rst_file_to_delete}")
rst_file_to_delete.unlink()
rst_file_to_delete.unlink(missing_ok=True)
namespace = namespace.rpartition('.')[0]
except CalledProcessError as e:
logging.error(
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
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()

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# ---------------------------------------------------------
# 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 import FoundryCBAgent
from .server.common.agent_run_context import AgentRunContext

config_logging()

__all__ = ["FoundryCBAgent", "AgentRunContext"]
__version__ = VERSION

# temporarily add build info here, remove it after public release
try:
from . import _buildinfo

__commit__ = _buildinfo.commit
__build_time__ = _buildinfo.build_time
except Exception: # pragma: no cover
__commit__ = "unknown"
__build_time__ = "unknown"
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# 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.
# --------------------------------------------------------------------------

from __future__ import annotations

import subprocess
from datetime import datetime, timezone
from typing import Optional

VERSION = "1.0.0"


def _from_buildinfo() -> tuple[Optional[str], Optional[str]]:
try:
from . import _buildinfo # type: ignore import-not-found
except Exception: # pragma: no cover - best effort fallback
return None, None

commit = getattr(_buildinfo, "commit", None)
build_time = getattr(_buildinfo, "build_time", None)
return commit or None, build_time or None


def _git_output(args: list[str]) -> Optional[str]:
try:
completed = subprocess.run(
["git", *args],
check=True,
capture_output=True,
text=True,
)
except Exception: # pragma: no cover - git may be unavailable
return None

output = completed.stdout.strip()
return output or None


def _coerce_date(date_str: Optional[str]) -> Optional[str]:
if not date_str:
return None

try:
# Accept ISO formatted timestamps with or without trailing Z
if date_str.endswith("Z"):
date_str = date_str[:-1] + "+00:00"
dt = datetime.fromisoformat(date_str)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
except ValueError:
return None

return dt.strftime("%y%m%d")


def _git_commit() -> Optional[str]:
return _git_output(["rev-parse", "--short=7", "HEAD"])


def _current_date_code() -> str:
return datetime.now(timezone.utc).strftime("%y%m%d")


def _format_version(base: str, commit: Optional[str], date: Optional[str]) -> str:
suffix_parts: list[str] = []

if commit:
suffix_parts.append(f"g{commit[:7]}")

if date:
suffix_parts.append(f"d{date}")

if suffix_parts:
return f"{base}+{'.'.join(suffix_parts)}"

return base


def _resolve_version() -> str:
build_commit, build_time = _from_buildinfo()
git_commit = _git_commit()
commit = git_commit or build_commit

date = _coerce_date(build_time)
if date is None:
date = _current_date_code()

return _format_version(VERSION, commit, date)


VERSION = _resolve_version()

__all__ = ["VERSION"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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"
150 changes: 150 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,150 @@
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}"
else:
print("environment variable AGENT_PROJECT_RESOURCE_ID not set.")
return None


def get_application_insights_connection_string():
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`.
"""
try:
config.dictConfig(log_config)

application_insights_connection_string = get_application_insights_connection_string()
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 azure.monitor.opentelemetry.exporter import AzureMonitorLogExporter
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

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}")
pass


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():
"""
If the logger is not already configured, it will be initialized with default settings.
"""
return logging.getLogger("azure.ai.agentshosting")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The contract models in this folder was generated using typespec compiler with @azure-tools/typespec-python package from the typespec defined here: https://github.com/Azure/agent-first-sdk/tree/rapida/workflow-api-spec/typespec
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from ._create_response import CreateResponse # type: ignore
from .projects import Response, ResponseStreamEvent

__all__ = ["CreateResponse", "Response", "ResponseStreamEvent"] # type: ignore[var-annotated]

Loading
Loading