Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 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
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
5 changes: 4 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,10 @@
"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
}
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.0a1 (2025-11-06)

### Features Added

First version
21 changes: 21 additions & 0 deletions sdk/ai/azure-ai-agentserver-core/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Copyright (c) Microsoft Corporation.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions sdk/ai/azure-ai-agentserver-core/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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
include azure/ai/agentserver/core/py.typed
119 changes: 119 additions & 0 deletions sdk/ai/azure-ai-agentserver-core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Azure AI Agent Server Adapter


## Getting started

```bash
pip install azure-ai-agentserver-core
```

## Key concepts

This is the core package for Azure AI Agent server. It hosts your agent as a container on the cloud.

You can talk to your agent using azure-ai-project sdk.


## Examples

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()

```

## Troubleshooting

First run your agent with azure-ai-agentserver-core locally.

If it works on local by failed on cloud. Check your logs in the application insight connected to your Azure AI Foundry Project.


### Reporting issues

To report an issue with the client library, or request additional features, please open a GitHub issue [here](https://github.com/Azure/azure-sdk-for-python/issues). Mention the package name "azure-ai-agents" in the title or content.


## Next steps

Please visit [Samples](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/ai/azure-ai-agentserver-core/samples) folder. There are several cases for you to build your agent with azure-ai-agentserver


## Contributing

This project welcomes contributions and suggestions. Most contributions require
you to agree to a Contributor License Agreement (CLA) declaring that you have
the right to, and actually do, grant us the rights to use your contribution.
For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether
you need to provide a CLA and decorate the PR appropriately (e.g., label,
comment). Simply follow the instructions provided by the bot. You will only
need to do this once across all repos using our CLA.

This project has adopted the
[Microsoft Open Source Code of Conduct][code_of_conduct]. For more information,
see the Code of Conduct FAQ or contact [email protected] with any
additional questions or comments.
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,12 @@
# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
# pylint: disable=no-name-in-module
from typing import Optional

from .openai import response_create_params # type: ignore
from . import projects as _azure_ai_projects_models

class CreateResponse(response_create_params.ResponseCreateParamsBase, total=False): # type: ignore
agent: Optional[_azure_ai_projects_models.AgentReference]
stream: Optional[bool]
Loading
Loading