Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
#![recursion_limit = "256"]
use activity::inbound::toolset::activity_toolset;
use agent_session::inbound::toolset::{ReadAgentSession, coding_agent_toolset};
use ai_toolset::AsyncToolCollection;
mod frontend_compat;
use ai_toolset::schema::{FrontendSchemas, ToolSchemaGenerator, frontend_schemas_builder};
#[cfg(test)]
mod test;
pub mod ai_operations;
mod build_context;
pub mod crm_csv_files;
mod deferred;
mod display_results;
pub mod email_attachments;
mod import_channels;
mod import_jobs;
mod import_trackers;
mod import_workspace;
mod mcp_app_catalog;
pub mod memory;
mod schemas;
pub mod search;
mod search_tools;
mod self_knowledge;
pub mod serde_utils;
mod subagent;
mod tool_context;
pub mod user_tool_review;
pub use anthropic::toolset::AnthropicToolContext;
use anthropic::toolset::anthropic_toolset;
use bots::inbound::toolset::bot_toolset;
use calendar_events::inbound::team_toolset::team_calendar_toolset;
use calendar_events::inbound::toolset::{calendar_toolset, mcp_toolset as calendar_mcp_toolset};
use calendar_scheduling::inbound::toolset::{
booking_link_toolset, booking_management_toolset, mcp_toolset as booking_link_mcp_toolset,
};
use call::inbound::toolset::call_toolset;
use channels::inbound::toolset::channel_toolset;
use cognition::inbound::toolset::cognition_toolset;
use crm::inbound::toolset::{crm_toolset, pipelines::pipeline_toolset};
use databases::inbound::toolset::{databases_read_only_toolset, databases_toolset};
use databases_sql::toolset::{QueryDatabase, databases_sql_toolset};
use display_results::DisplayResults;
use documents::inbound::toolset::document_toolset;
use email::inbound::toolset::{email_toolset, mcp_toolset as email_mcp_toolset};
use favorites::inbound::toolset::favorites_toolset;
use import::inbound::toolset::import_toolset;
use initiative::inbound::toolset::initiative_toolset;
use mcp_client::inbound::toolset::mcp_server_toolset;
use messages::inbound::toolset::message_toolset;
use notification::inbound::ai_tool::{item_notification_preference_toolset, notification_toolset};
use projects::inbound::toolset::project_toolset;
use properties::inbound::toolset::properties_toolset;
use schemas::read;
use search_tools::{LoadTools, SearchTools};
use self_knowledge::SelfKnowledge;
use skills::inbound::toolset::skill_toolset;
use soup::inbound::toolset::{ListEntities, SoupToolContext};
use std::sync::Arc;
use subagent::{Subagent, SubagentContext};
use teams::inbound::toolset::team_toolset;
#[cfg(any(test, feature = "test-support"))]
pub use build_context::build_anthropic_tool_context_test;
pub use build_context::{
build_anthropic_tool_context, build_cognition_tool_context, build_image_generator_from_env,
build_tool_service_context_from_env, build_web_search_provider_from_env,
};
pub use deferred::{DeferredToolSet, deferred_tools};
pub use import_jobs::{IMPORT_JOBS_HANDOVER_TIMEOUT, ImportJobs};
pub use mcp_app_catalog::{
PipedreamMcpAppCatalog, canvas_artifacts_rollout, canvas_artifacts_rollout_from_env,
connect_adapter, connector_backends_from_env, macro_connect_rollout, pipedream_client_from_env,
};
pub use search::search_toolset;
pub use tool_context::{
ChannelSideEffectClients, FormsToolConfig, MaybeToolEventBroker, NoOpCallRtcClient,
NoOpConnectionService, NoOpNotificationIngress, NoOpNotificationService,
NoOpSnsEndpointManager, NoOpTaskProperties, RequestContext, RoutineToolContext,
TaskPropertiesAdapter, ToolActivityToolContext, ToolBookingLinkService,
ToolBookingLinkToolContext, ToolBookingManagementToolContext, ToolBotService,
ToolBotToolContext, ToolCalendarMutationService, ToolCalendarReadService,
ToolCalendarToolContext, ToolCallRecordQueryService, ToolCallService, ToolCallToolContext,
ToolChannelEventDispatcher, ToolChannelMediaUploadToolContext, ToolChannelMessagesService,
ToolChannelToolContext, ToolChatService, ToolChatToolContext, ToolCodingAgentToolContext,
ToolCognitionToolContext, ToolCommsService, ToolCrmService, ToolCrmToolContext,
ToolCrmTransferService, ToolCrmTransferToolContext, ToolDatabasesService,
ToolDatabasesSqlToolContext, ToolDatabasesToolContext, ToolDocumentService,
ToolDocumentToolContext, ToolEmailService, ToolEmailToolContext,
ToolEntityAccessManagementService, ToolEntityAccessService, ToolEntityCreator,
ToolFavoritesService, ToolFavoritesToolContext, ToolForeignEntityService, ToolFormsToolContext,
ToolFrecencyService, ToolGithubPullRequestService, ToolImageGenerationToolContext,
ToolImportService, ToolImportToolContext, ToolInitiativeToolContext,
ToolItemNotificationPreferenceToolContext, ToolMcpSelector, ToolMcpServerToolContext,
ToolMessageToolContext, ToolNotificationQueue, ToolNotificationService,
ToolNotificationToolContext, ToolPipedreamConnection, ToolPipelineService,
ToolPipelineToolContext, ToolPipelineWorkflows, ToolProjectService, ToolProjectToolContext,
ToolPropertiesService, ToolPropertiesToolContext, ToolReadAgentSessionContext,
ToolServiceContext, ToolSkillService, ToolSkillToolContext, ToolSoupService,
ToolSystemPropertiesService, ToolTableEventPublisher, ToolTeamCalendarService,
ToolTeamCalendarToolContext, ToolTeamService, ToolTeamToolContext, ToolUserEmailService,
ToolViewOnlyDatabasesSqlToolContext, build_activity_tool_context,
build_booking_link_tool_context, build_booking_management_tool_context, build_bot_tool_context,
build_calendar_tool_context, build_channel_media_upload_tool_context,
build_channel_tool_context_with_dispatcher, build_channel_tool_context_with_side_effects,
build_channel_tool_context_without_side_effects, build_coding_agent_tool_context,
build_crm_tool_context, build_crm_transfer_tool_context, build_databases_sql_tool_context,
build_databases_tool_context, build_favorites_service, build_favorites_tool_context,
build_forms_tool_context, build_image_generation_tool_context, build_import_repo,
build_initiative_tool_context, build_item_notification_preference_tool_context,
build_message_service_with_side_effects, build_message_service_without_side_effects,
build_message_tool_context, build_pipeline_tool_context, build_project_tool_context,
build_properties_service, build_properties_service_with_broker, build_properties_tool_context,
build_read_agent_session_context, build_routine_tool_context, build_skill_tool_context,
build_task_properties_adapter, build_team_calendar_tool_context, build_team_repository,
build_team_tool_context,
};
pub use tool_context::{ToolConnectorToolContext, build_connector_tool_context};
#[cfg(any(test, feature = "test-support"))]
pub use tool_context::{
build_channel_media_upload_tool_context_test, build_image_generation_tool_context_test,
no_op_schedule_context,
};
pub type AiToolSet = AsyncToolCollection<ToolServiceContext>;
/// Database-only capabilities for the in-app database assistant. This excludes
/// connectors and unrelated tools such as messaging and email.
pub fn database_tools() -> AiToolSet {
AsyncToolCollection::new()
.add_subtoolset::<ToolDatabasesToolContext>(databases_toolset())
.add_subtoolset::<ToolDatabasesSqlToolContext>(databases_sql_toolset())
}
/// Discovery and QueryDatabase for live document answers. The SQL tool is
/// the one every host gets; here it runs over access capped at view, so the
/// access check refuses its writes even for a user who could edit.
pub fn database_read_only_tools() -> AiToolSet {
AsyncToolCollection::new()
.add_subtoolset::<ToolDatabasesToolContext>(databases_read_only_toolset())
.add_tool::<QueryDatabase, ToolViewOnlyDatabasesSqlToolContext>()
}
pub struct ToolSetWithPrompt {
pub toolset: Arc<AiToolSet>,
/// The prompt with how each area of Macro works inline, generated from the
/// SDK's `@agent` guidance.
pub prompt: Box<dyn std::fmt::Display + Send + Sync>,
/// The prompt for turns that route SDK entities, whose selected entities
/// bring their guidance into the turn; [`Self::prompt`] on hosts that do
/// not route.
pub routed_prompt: Box<dyn std::fmt::Display + Send + Sync>,
/// The tools a [`DeferredToolSet`] keeps out of every request; the prompt
/// lists them. Empty on hosts that send every schema.
pub deferred: Arc<[ai_toolset::SearchableTool]>,
}
impl ToolSchemaGenerator for ToolSetWithPrompt {
fn register_schemas(
&self,
generator: &mut schemars::SchemaGenerator,
) -> Vec<ai_toolset::schema::FrontendToolEntry> {
self.toolset.register_schemas(generator)
}
}
/// Toolset available to subagents — everything except email and the Subagent
/// tool itself (subagents cannot create subagents).
pub(crate) fn subagent_toolset() -> AiToolSet {
AsyncToolCollection::new()
.add_toolset(search_toolset())
.add_tool::<SelfKnowledge, ToolServiceContext>()
.add_tool::<ListEntities, SoupToolContext<ToolSoupService, ToolEmailService>>()
.add_subtoolset::<ToolActivityToolContext>(activity_toolset())
.add_subtoolset::<ToolDocumentToolContext>(document_toolset())
.add_subtoolset::<ToolImageGenerationToolContext>(
image_generation::inbound::toolset::image_generation_toolset(),
)
.add_subtoolset::<ToolCognitionToolContext>(cognition_toolset())
.add_subtoolset::<ToolProjectToolContext>(project_toolset())
.add_subtoolset::<ToolFavoritesToolContext>(favorites_toolset())
.add_subtoolset::<ToolMessageToolContext>(message_toolset())
.add_subtoolset::<ToolInitiativeToolContext>(initiative_toolset())
.add_subtoolset::<ToolPropertiesToolContext>(properties_toolset())
.add_subtoolset::<ToolCallToolContext>(call_toolset())
.add_tool::<ReadAgentSession, ToolReadAgentSessionContext>()
.add_tool::<agent_session::inbound::toolset::ReadAgentSessionSharing, ToolReadAgentSessionContext>()
.add_tool::<agent_session::inbound::toolset::UpdateAgentSessionSharing, ToolReadAgentSessionContext>()
.add_subtoolset::<ToolChannelToolContext>(channel_toolset())
.add_subtoolset::<ToolBotToolContext>(bot_toolset())
.add_subtoolset::<ToolTeamToolContext>(team_toolset())
.add_subtoolset::<ToolCrmToolContext>(crm_toolset())
.add_subtoolset::<ToolPipelineToolContext>(pipeline_toolset())
.add_subtoolset::<ToolCrmTransferToolContext>(
crm::inbound::toolset::transfer::crm_transfer_toolset(),
)
.add_subtoolset::<ToolDatabasesToolContext>(databases_toolset())
.add_subtoolset::<ToolFormsToolContext>(forms::inbound::toolset::authoring_toolset())
.add_subtoolset::<ToolDatabasesSqlToolContext>(databases_sql_toolset())
.add_subtoolset::<ToolSkillToolContext>(skill_toolset())
.add_subtoolset::<AnthropicToolContext>(anthropic_toolset())
.add_subtoolset::<web_search::inbound::toolset::WebToolContext>(
web_search::inbound::toolset::web_toolset(),
)
}
/// The host a toolset is assembled for.
///
/// Hosts differ on two axes. First, whether something finishes a deferred
/// user tool for them: [`AiHost::Chat`] has the composer card after the
/// turn and [`AiHost::AgentSession`] the review elicitation in it, so those
/// two register `SendEmail` and the deferring `CreateCalendarEvent` — on any
/// other host those registrations would return `PendingUserExecution`
/// forever while reading to the model as success. Second, whether the host
/// supports tool discovery and can render tool-call views. Channel bots keep
/// discovery, but only chat and agent-session transcripts render rich views.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AiHost {
/// The AI chat, and any host whose conversation is stored as a chat the
/// frontend can render (scheduled agents, memory generation): composer
/// cards finish deferred user tools there, after the turn.
Chat,
/// Macro's in-process agent serving an agent session: the same toolset
/// as [`AiHost::Chat`], but its user tools are reviewed in the turn - the
/// agent loop's finisher puts the call to the user over ACP and the tool
/// returns the outcome - so the prompt describes a review card the agent
/// waits on, never a pending composer. It presents views with the
/// harness's `execution.views.present` instead of `DisplayResults`.
AgentSession,
/// The channel-mention bot: no composer, so `CreateCalendarEvent`
/// executes directly in the agent loop and `SendEmail` is omitted. Replies
/// contain text only, so this host also omits `DisplayResults`.
ChannelBot,
/// The MCP server: like [`AiHost::ChannelBot`] for user tools — MCP
/// clients apply their own confirmation policy from tool annotations —
/// and without the chat frontend's discovery/display tools.
Mcp,
}
/// The tools whose schemas go out with every request on hosts with tool
/// search: the ones most turns use. Every other tool is deferred (see
/// [`DeferredToolSet`]), listed by name and summary in the prompt and loaded
/// with `LoadTools` when needed.
pub const EAGER_TOOLS: &[&str] = &[
"BashCodeExecution",
"ContentSearch",
"CreateDocument",
"DisplayResults",
"DiscoverConnectors",
"EditDocument",
"GetThread",
"ListEntities",
"ListSkills",
"LoadTools",
"NameSearch",
"ReadChannelMessageContext",
"ReadAgentSession",
"ReadChannelMessages",
"ReadChannelThread",
"ReadContent",
"ReadMetadata",
"ReadSkill",
"SearchSkills",
"SearchTools",
"SelfKnowledge",
"SendChannelMessage",
"ShareWithChannel",
"Subagent",
"TextEditorCodeExecution",
"WebFetch",
"WebSearch",
];
/// Assemble the toolset and tool-use prompt for a host. These are actually
/// sent to the AI provider.
pub fn tools_for(host: AiHost) -> ToolSetWithPrompt {
tools_for_inner(host, false)
}
/// Direct tools retained when an agent can use the full Macro SDK through code.
/// Filtering happens before discovery and prompt construction, so lazy loading
/// cannot reintroduce the hidden SDK methods as direct tools.
pub fn tools_for_agent_code_mode() -> ToolSetWithPrompt {
tools_for_inner(AiHost::AgentSession, true)
}
/// Existing agent-session tools exposed through the code-mode capability policy.
pub fn code_mode_tools() -> Arc<AiToolSet> {
tools_for(AiHost::AgentSession).toolset
}
fn tools_for_inner(host: AiHost, code_mode_direct: bool) -> ToolSetWithPrompt {
let toolset = subagent_toolset()
.add_subtoolset::<memory::ToolMemoryContext>(::memory::inbound::toolset::memory_toolset())
.add_subtoolset::<ToolBookingManagementToolContext>(booking_management_toolset())
.add_subtoolset::<ToolNotificationToolContext>(notification_toolset())
.add_subtoolset::<ToolItemNotificationPreferenceToolContext>(
item_notification_preference_toolset(),
)
.add_subtoolset::<ToolTeamCalendarToolContext>(team_calendar_toolset())
.add_subtoolset::<RoutineToolContext>(routines::inbound::routine_toolset());
let toolset = match host {
AiHost::Chat | AiHost::AgentSession => toolset
.add_subtoolset::<ToolEmailToolContext>(email_toolset())
.add_subtoolset::<ToolCalendarToolContext>(calendar_toolset())
.add_subtoolset::<ToolBookingLinkToolContext>(booking_link_toolset())
.add_tool::<forms::inbound::toolset::SetFormAccess, ToolFormsToolContext>(),
AiHost::ChannelBot | AiHost::Mcp => toolset
.add_subtoolset::<ToolEmailToolContext>(email_mcp_toolset())
.add_subtoolset::<ToolCalendarToolContext>(calendar_mcp_toolset())
.add_subtoolset::<ToolBookingLinkToolContext>(booking_link_mcp_toolset())
.add_tool::<forms::inbound::toolset::SetFormAccess, ToolFormsToolContext>(),
};
let toolset = toolset
.add_subtoolset::<ToolImportToolContext>(import_toolset())
.add_subtoolset::<ToolCodingAgentToolContext>(coding_agent_toolset())
.add_tool::<Subagent, SubagentContext>();
// Only Macro's own agents connect servers for the user.
let toolset = match host {
AiHost::Chat | AiHost::AgentSession => {
toolset.add_subtoolset::<ToolMcpServerToolContext>(mcp_server_toolset::<
mcp_client::outbound::NativeServerStore,
>())
}
AiHost::ChannelBot | AiHost::Mcp => toolset,
};
let toolset = match host {
AiHost::Chat | AiHost::AgentSession | AiHost::ChannelBot => toolset
.add_tool::<SearchTools, ToolServiceContext>()
.add_tool::<LoadTools, ToolServiceContext>(),
AiHost::Mcp => toolset,
};
// Agent sessions present views with the harness's `execution.views.present`,
// which checks a view before it is drawn and posts it with a DM reply.
let toolset = match host {
AiHost::Chat => toolset.add_tool::<DisplayResults, ToolServiceContext>(),
AiHost::AgentSession | AiHost::ChannelBot | AiHost::Mcp => toolset,
};
// A direct upload needs a client that can PUT the bytes itself; in-app
// hosts have no network-capable shell, so only MCP clients get it.
let toolset = match host {
AiHost::Mcp => toolset.add_tool::<
channels::inbound::toolset::CreateChannelMediaUpload,
ToolChannelMediaUploadToolContext,
>(),
AiHost::Chat | AiHost::AgentSession | AiHost::ChannelBot => toolset,
};
// Connector discovery belongs to agent sessions. Their runtime refreshes
// the permitted connector list before the next turn after authorization.
let toolset = match host {
AiHost::AgentSession => toolset.add_tool::<
pipedream_mcp::inbound::toolset::DiscoverConnectors,
ToolConnectorToolContext,
>(),
AiHost::Chat | AiHost::ChannelBot | AiHost::Mcp => toolset,
};
let mut toolset = toolset;
if code_mode_direct {
// Review submission still needs these user-tool implementations internally,
// but advertising their blocking agent calls would reintroduce elicitation.
toolset.tools.retain(|name, _| {
!matches!(name.as_str(), "SendEmail" | "CreateCalendarEvent")
&& !agent_code_mode::domain::covered_by_code_sdk(
name,
toolset.user_tools.contains_key(name),
)
});
}
// External MCP clients have no `LoadTools`, so they get every schema.
let deferred = match host {
AiHost::Chat | AiHost::AgentSession | AiHost::ChannelBot => {
deferred_tools(&toolset, EAGER_TOOLS)
}
AiHost::Mcp => Arc::from([]),
};
use agent_code_mode::domain::{GuideHost, inline_guides};
// Code mode starts agent sessions through the SDK, whose guidance says
// how; direct hosts keep the dispatch tools' own guidance.
let (tools_prompt, guides): (String, &str) = match host {
AiHost::Chat => (
prompt::TOOL_USE_PROMPT
.compose(&prompt::coding_agents::PROMPT)
.to_string(),
inline_guides(GuideHost::Direct),
),
AiHost::AgentSession if code_mode_direct => (
prompt::CODE_MODE_SESSION_TOOL_USE_PROMPT.to_string(),
inline_guides(GuideHost::Code),
),
AiHost::AgentSession => (
prompt::SESSION_TOOL_USE_PROMPT
.compose(&prompt::coding_agents::PROMPT)
.to_string(),
inline_guides(GuideHost::Direct),
),
AiHost::ChannelBot | AiHost::Mcp => (
prompt::DIRECT_TOOL_USE_PROMPT
.compose(&prompt::coding_agents::PROMPT)
.to_string(),
inline_guides(GuideHost::Direct),
),
};
let catalog: Vec<(&str, &str)> = deferred
.iter()
.map(|tool| (tool.name.as_str(), tool.description.as_str()))
.collect();
let deferred_section = prompt::deferred_tools::render(&catalog).unwrap_or_default();
let prompt = format!("{tools_prompt}{guides}{deferred_section}");
// Only code-mode sessions route SDK entities per turn.
let routed_prompt = if code_mode_direct {
format!("{tools_prompt}{deferred_section}")
} else {
prompt.clone()
};
ToolSetWithPrompt {
toolset: Arc::new(toolset),
prompt: Box::new(prompt),
routed_prompt: Box::new(routed_prompt),
deferred,
}
}
/// Frontend typegen schemas with shared, deduplicated `$defs`.
///
/// These feed `gen_tool_schemas` / `generate-dcs-tools.ts` and are never
/// sent to AI providers.
pub fn all_tool_frontend_schemas() -> FrontendSchemas {
frontend_schemas_builder()
.merge(&tools_for(AiHost::AgentSession))
.merge(&agent_code_mode::inbound::toolset::toolset())
// Persisted legacy calls still render, but are never executable or advertised.
.merge(&chat::inbound::toolset::chat_toolset::<
ToolChatService,
ToolEntityAccessService,
>())
.merge(&read::read_thread())
.merge(&frontend_compat::LegacyConversationSchemas)
.build()
}
pub fn no_tools() -> ToolSetWithPrompt {
ToolSetWithPrompt {
prompt: Box::new(&prompt::BASE_PROMPT),
routed_prompt: Box::new(&prompt::BASE_PROMPT),
toolset: Arc::new(AsyncToolCollection::new()),
deferred: Arc::from([]),
}
}